You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by kh...@apache.org on 2016/07/16 12:05:01 UTC

maven git commit: [MNG-6012] Missing profile is only notified at the end of a run o Introduced new command line option --fail-on-missing-profiles which will fail the build immediately. o Print WARNING at the beginning and at the end of the output.

Repository: maven
Updated Branches:
  refs/heads/MNG-6012-Missing-Profile-At-End [created] 7bf0a63d5


[MNG-6012]�Missing profile is only notified at the end of a run
 o Introduced new command line option --fail-on-missing-profiles
   which will fail the build immediately.
 o Print WARNING at the beginning and at the end of the output.


Project: http://git-wip-us.apache.org/repos/asf/maven/repo
Commit: http://git-wip-us.apache.org/repos/asf/maven/commit/7bf0a63d
Tree: http://git-wip-us.apache.org/repos/asf/maven/tree/7bf0a63d
Diff: http://git-wip-us.apache.org/repos/asf/maven/diff/7bf0a63d

Branch: refs/heads/MNG-6012-Missing-Profile-At-End
Commit: 7bf0a63d5bfc2384430dbc145f4174042c281532
Parents: 2270852
Author: Karl Heinz Marbaise <kh...@apache.org>
Authored: Sat Jul 16 13:13:54 2016 +0200
Committer: Karl Heinz Marbaise <kh...@apache.org>
Committed: Sat Jul 16 13:58:15 2016 +0200

----------------------------------------------------------------------
 .../java/org/apache/maven/DefaultMaven.java     | 56 +++++++++++++++++---
 .../execution/DefaultMavenExecutionRequest.java | 15 ++++++
 .../maven/execution/MavenExecutionRequest.java  | 15 ++++++
 .../java/org/apache/maven/cli/CLIManager.java   |  3 ++
 .../java/org/apache/maven/cli/MavenCli.java     |  5 ++
 5 files changed, 88 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven/blob/7bf0a63d/maven-core/src/main/java/org/apache/maven/DefaultMaven.java
----------------------------------------------------------------------
diff --git a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java
index da17830..7051737 100644
--- a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java
+++ b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java
@@ -28,6 +28,7 @@ import java.util.Date;
 import java.util.HashSet;
 import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 
@@ -45,6 +46,7 @@ import org.apache.maven.lifecycle.internal.LifecycleStarter;
 import org.apache.maven.model.building.ModelProblem;
 import org.apache.maven.model.building.Result;
 import org.apache.maven.plugin.LegacySupport;
+import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.project.MavenProject;
 import org.apache.maven.project.ProjectBuilder;
 import org.apache.maven.repository.LocalRepositoryNotAccessibleException;
@@ -304,9 +306,28 @@ public class DefaultMaven
 
             result.setProject( session.getTopLevelProject() );
 
+            // Give the user a hint about not existing profiles at the beginning and fail
+            // the build if he request to do so.
+            List<String> profilesWhichDoNotExist = identifyProfilesWhichDoNotExist( session.getProjects(), request.getActiveProfiles() );
+            if ( !profilesWhichDoNotExist.isEmpty() )
+            {
+                if ( request.isFailOnMissingProfiles() )
+                {
+                    logWarnOrError( profilesWhichDoNotExist, true );
+
+                    addExceptionToResult( result, new MojoFailureException(
+                        "One or more profiles you have requested to be activated do not exist." ) );
+                    return result;
+                }
+                else
+                {
+                    logWarnOrError( profilesWhichDoNotExist, false );
+                }
+            }
+
             lifecycleStarter.execute( session );
 
-            validateActivatedProfiles( session.getProjects(), request.getActiveProfiles() );
+            logWarnOrError( profilesWhichDoNotExist, false );
 
             if ( session.getResult().hasExceptions() )
             {
@@ -328,6 +349,27 @@ public class DefaultMaven
         return result;
     }
 
+    private void logWarnOrError( List<String> profilesWhichDoNotExist, boolean error )
+    {
+        for ( String profileId : profilesWhichDoNotExist )
+        {
+            StringBuilder sb = new StringBuilder( "The requested profile " );
+            sb.append( '"' );
+            sb.append( profileId );
+            sb.append( '"' );
+            sb.append( " could not be activated because it does not exist." );
+
+            if ( error )
+            {
+                logger.error( sb.toString() );
+            }
+            else
+            {
+                logger.warn( sb.toString() );
+            }
+        }
+    }
+
     private void afterSessionEnd( Collection<MavenProject> projects, MavenSession session )
         throws MavenExecutionException
     {
@@ -425,8 +467,10 @@ public class DefaultMaven
         return result;
     }
 
-    private void validateActivatedProfiles( List<MavenProject> projects, List<String> activeProfileIds )
+    private List<String> identifyProfilesWhichDoNotExist( List<MavenProject> projects, List<String> activeProfileIds )
     {
+        List<String> result = new LinkedList<>();
+        
         Collection<String> notActivatedProfileIds = new LinkedHashSet<>( activeProfileIds );
 
         for ( MavenProject project : projects )
@@ -437,11 +481,11 @@ public class DefaultMaven
             }
         }
 
-        for ( String notActivatedProfileId : notActivatedProfileIds )
-        {
-            logger.warn( "The requested profile \"" + notActivatedProfileId
-                + "\" could not be activated because it does not exist." );
+        if ( !notActivatedProfileIds.isEmpty() ) {
+            result.addAll( notActivatedProfileIds );
         }
+
+        return result;
     }
 
     private Map<String, MavenProject> getProjectMap( Collection<MavenProject> projects )

http://git-wip-us.apache.org/repos/asf/maven/blob/7bf0a63d/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequest.java
----------------------------------------------------------------------
diff --git a/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequest.java b/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequest.java
index 71a6894..a95a7f2 100644
--- a/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequest.java
+++ b/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequest.java
@@ -163,6 +163,8 @@ public class DefaultMavenExecutionRequest
     private boolean useLegacyLocalRepositoryManager = false;
 
     private Map<String, Object> data;
+    
+    private boolean failOnMissingProfiles;
 
     public DefaultMavenExecutionRequest()
     {
@@ -1285,4 +1287,17 @@ public class DefaultMavenExecutionRequest
 
         return data;
     }
+
+    @Override
+    public boolean isFailOnMissingProfiles()
+    {
+        return failOnMissingProfiles;
+    }
+    
+    @Override
+    public MavenExecutionRequest setFailOnMissingProfiles( boolean failOnMissingProfiles )
+    {
+        this.failOnMissingProfiles = failOnMissingProfiles;
+        return this;
+    }
 }

http://git-wip-us.apache.org/repos/asf/maven/blob/7bf0a63d/maven-core/src/main/java/org/apache/maven/execution/MavenExecutionRequest.java
----------------------------------------------------------------------
diff --git a/maven-core/src/main/java/org/apache/maven/execution/MavenExecutionRequest.java b/maven-core/src/main/java/org/apache/maven/execution/MavenExecutionRequest.java
index 1ec8966..335cc61 100644
--- a/maven-core/src/main/java/org/apache/maven/execution/MavenExecutionRequest.java
+++ b/maven-core/src/main/java/org/apache/maven/execution/MavenExecutionRequest.java
@@ -442,4 +442,19 @@ public interface MavenExecutionRequest
      * @since 3.3.0
      */
     Map<String, Object> getData();
+    
+    /**
+     * @return {@code true} if we should fail on missing profiles {@code false} otherwise.
+     * @since 3.4.0
+     */
+    boolean isFailOnMissingProfiles();
+
+    /**
+     * {@code true} to fail the build on missing profiles {@code false} otherwise.
+     * @param failOnMissingProfiles true/false.
+     * @return {@link MavenExecutionRequest}
+     * @since 3.4.0
+     */
+    MavenExecutionRequest setFailOnMissingProfiles( boolean failOnMissingProfiles );
+
 }

http://git-wip-us.apache.org/repos/asf/maven/blob/7bf0a63d/maven-embedder/src/main/java/org/apache/maven/cli/CLIManager.java
----------------------------------------------------------------------
diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/CLIManager.java b/maven-embedder/src/main/java/org/apache/maven/cli/CLIManager.java
index f461835..f10cc61 100644
--- a/maven-embedder/src/main/java/org/apache/maven/cli/CLIManager.java
+++ b/maven-embedder/src/main/java/org/apache/maven/cli/CLIManager.java
@@ -80,6 +80,8 @@ public class CLIManager
     public static final String FAIL_AT_END = "fae";
 
     public static final String FAIL_NEVER = "fn";
+    
+    public static final String FAIL_ON_MISSING_PROFILES = "fomp";
 
     public static final String RESUME_FROM = "rf";
 
@@ -140,6 +142,7 @@ public class CLIManager
         options.addOption( OptionBuilder.withLongOpt( "threads" ).hasArg().withDescription( "Thread count, for instance 2.0C where C is core multiplied" ).create( THREADS ) );
         options.addOption( OptionBuilder.withLongOpt( "legacy-local-repository" ).withDescription( "Use Maven 2 Legacy Local Repository behaviour, ie no use of _remote.repositories. Can also be activated by using -Dmaven.legacyLocalRepo=true" ).create( LEGACY_LOCAL_REPOSITORY ) );
         options.addOption( OptionBuilder.withLongOpt( "builder" ).hasArg().withDescription( "The id of the build strategy to use." ).create( BUILDER ) );
+        options.addOption( OptionBuilder.withLongOpt( "fail-on-missing-profiles" ).withDescription( "The build will fail if you are trying to use one or more profiles which do not exist." ).create( FAIL_ON_MISSING_PROFILES ) );
 
         // Adding this back in for compatibility with the verifier that hard codes this option.
         options.addOption( OptionBuilder.withLongOpt( "no-plugin-registry" ).withDescription( "Ineffective, only kept for backward compatibility" ).create( "npr" ) );

http://git-wip-us.apache.org/repos/asf/maven/blob/7bf0a63d/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
----------------------------------------------------------------------
diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
index 3e1194b..c8dc20b 100644
--- a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
+++ b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
@@ -1592,6 +1592,11 @@ public class MavenCli
             }
         }
 
+        if ( commandLine.hasOption( CLIManager.FAIL_ON_MISSING_PROFILES ) )
+        {
+            request.setFailOnMissingProfiles( true );
+        }
+
         //
         // Allow the builder to be overriden by the user if requested. The builders are now pluggable.
         //