You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by rf...@apache.org on 2018/09/22 15:40:43 UTC

[maven] 01/01: [MNG-5667] Either install or deploy

This is an automated email from the ASF dual-hosted git repository.

rfscholte pushed a commit to branch MNG-5667
in repository https://gitbox.apache.org/repos/asf/maven.git

commit baf5036e5a01a70eab3cbd28778ae21358d9c179
Author: rfscholte <rf...@apache.org>
AuthorDate: Sat Sep 22 17:40:35 2018 +0200

    [MNG-5667] Either install or deploy
---
 .../java/org/apache/maven/lifecycle/Choice.java    | 52 +++++++++++++++
 .../java/org/apache/maven/lifecycle/Lifecycle.java | 51 ++++++++++++---
 .../org/apache/maven/lifecycle/LifecyclePhase.java | 30 +++++++++
 .../apache/maven/lifecycle/LifecycleResolver.java  | 57 ++++++++++++++++
 .../java/org/apache/maven/lifecycle/Phase.java     | 76 ++++++++++++++++++++++
 .../org/apache/maven/lifecycle/PhasesWrapper.java  | 49 ++++++++++++++
 .../internal/DefaultLifecycleMappingDelegate.java  | 17 +++--
 .../main/resources/META-INF/plexus/components.xml  |  8 ++-
 .../maven/lifecycle/LifecycleResolverTest.java     | 59 +++++++++++++++++
 .../internal/stub/DefaultLifecyclesStub.java       | 31 ++++++---
 10 files changed, 400 insertions(+), 30 deletions(-)

diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/Choice.java b/maven-core/src/main/java/org/apache/maven/lifecycle/Choice.java
new file mode 100644
index 0000000..d4c1470
--- /dev/null
+++ b/maven-core/src/main/java/org/apache/maven/lifecycle/Choice.java
@@ -0,0 +1,52 @@
+package org.apache.maven.lifecycle;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.List;
+
+/**
+ * Selectable LifecyclePhase implementation. Pick one of the phases
+ * 
+ * @author Robert Scholte
+ * @since 3.6.0
+ */
+public class Choice implements LifecyclePhase
+{
+    private List<Phase> phases;
+    
+    @Override
+    public Phase resolve( String phase )
+    {
+        for ( LifecyclePhase lifecyclePhase : phases )
+        {
+            Phase resolvedPhase = lifecyclePhase.resolve( phase ); 
+            if ( phase.equals( resolvedPhase.getValue() ) )
+            {
+                return resolvedPhase;
+            }
+        }
+        return null; // or throw exception??
+    }
+
+    public List<Phase> getPhases()
+    {
+        return phases;
+    }
+}
diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/Lifecycle.java b/maven-core/src/main/java/org/apache/maven/lifecycle/Lifecycle.java
index 5fc99d5..1c1193b 100644
--- a/maven-core/src/main/java/org/apache/maven/lifecycle/Lifecycle.java
+++ b/maven-core/src/main/java/org/apache/maven/lifecycle/Lifecycle.java
@@ -19,11 +19,10 @@ package org.apache.maven.lifecycle;
  * under the License.
  */
 
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
-import org.apache.maven.lifecycle.mapping.LifecyclePhase;
-
 /**
  * Class Lifecycle.
  */
@@ -33,10 +32,11 @@ public class Lifecycle
     {
     }
 
-    public Lifecycle( String id, List<String> phases, Map<String, LifecyclePhase> defaultPhases )
+    public Lifecycle( String id, List<LifecyclePhase> phases,
+                      Map<String, org.apache.maven.lifecycle.mapping.LifecyclePhase> defaultPhases )
     {
         this.id = id;
-        this.phases = phases;
+        this.phases.getPhases().addAll( phases );
         this.defaultPhases = defaultPhases;
     }
 
@@ -54,21 +54,53 @@ public class Lifecycle
 
     private String id;
 
-    private List<String> phases;
+    // Must be called phases for backward compatibility; use to be List<String>
+    private PhasesWrapper phases = new PhasesWrapper();
 
-    private Map<String, LifecyclePhase> defaultPhases;
+    private Map<String, org.apache.maven.lifecycle.mapping.LifecyclePhase> defaultPhases;
 
     public String getId()
     {
         return this.id;
     }
 
+    /**
+     * Flattened (original) lifecycle
+     * 
+     * @return
+     */
     public List<String> getPhases()
     {
-        return this.phases;
+        List<String> names = new ArrayList<>();
+        for ( LifecyclePhase phase : this.phases.getPhases() )
+        {
+            if ( phase instanceof Phase )
+            {
+                names.add( ( (Phase) phase ).getValue() );
+            }
+            else if ( phase instanceof Choice )
+            {
+                Choice choice = (Choice) phase;
+                for ( Phase choicePhase : choice.getPhases() )
+                {
+                    names.add( choicePhase.getValue() );
+                }
+            }
+        }
+        return names;
+    }
+    
+    /**
+     * Structured lifecycle
+     * 
+     * @return
+     */
+    public List<LifecyclePhase> phases()
+    {
+        return phases.getPhases();
     }
 
-    public Map<String, LifecyclePhase> getDefaultLifecyclePhases()
+    public Map<String, org.apache.maven.lifecycle.mapping.LifecyclePhase> getDefaultLifecyclePhases()
     {
         return defaultPhases;
     }
@@ -76,7 +108,7 @@ public class Lifecycle
     @Deprecated
     public Map<String, String> getDefaultPhases()
     {
-        return LifecyclePhase.toLegacyMap( getDefaultLifecyclePhases() );
+        return org.apache.maven.lifecycle.mapping.LifecyclePhase.toLegacyMap( getDefaultLifecyclePhases() );
     }
 
     @Override
@@ -84,5 +116,4 @@ public class Lifecycle
     {
         return id + " -> " + phases;
     }
-
 }
diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/LifecyclePhase.java b/maven-core/src/main/java/org/apache/maven/lifecycle/LifecyclePhase.java
new file mode 100644
index 0000000..50e56dd
--- /dev/null
+++ b/maven-core/src/main/java/org/apache/maven/lifecycle/LifecyclePhase.java
@@ -0,0 +1,30 @@
+package org.apache.maven.lifecycle;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ * 
+ * @author Robert Scholte
+ * @since 3.6.0
+ */
+public interface LifecyclePhase
+{
+    Phase resolve( String phase );
+}
diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/LifecycleResolver.java b/maven-core/src/main/java/org/apache/maven/lifecycle/LifecycleResolver.java
new file mode 100644
index 0000000..abcfdb1
--- /dev/null
+++ b/maven-core/src/main/java/org/apache/maven/lifecycle/LifecycleResolver.java
@@ -0,0 +1,57 @@
+package org.apache.maven.lifecycle;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.codehaus.plexus.component.annotations.Component;
+
+/**
+ * @author Robert Scholte
+ * @since 3.6.0
+ */
+@Component( role = LifecycleResolver.class )
+public class LifecycleResolver
+{
+
+    /**
+     * @param lifecycle the lifecycle
+     * @param phase the target phase
+     * @return all phases up until the target phase
+     */
+    public List<Phase> resolve( Lifecycle lifecycle, String phase )
+    {
+        List<Phase> phases = new ArrayList<>( lifecycle.getPhases().size() );
+        
+        for ( LifecyclePhase lp : lifecycle.phases() )
+        {
+            Phase resolvedPhase = lp.resolve( phase );
+            phases.add( resolvedPhase );
+            
+            if ( phase.equals( resolvedPhase.getValue() ) )
+            {
+                break;
+            }
+        }
+        
+        return phases;
+    }
+}
diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/Phase.java b/maven-core/src/main/java/org/apache/maven/lifecycle/Phase.java
new file mode 100644
index 0000000..b725690
--- /dev/null
+++ b/maven-core/src/main/java/org/apache/maven/lifecycle/Phase.java
@@ -0,0 +1,76 @@
+package org.apache.maven.lifecycle;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.Objects;
+
+/**
+ * Standard LifecyclePhase implementation
+ * 
+ * @author Robert Scholte
+ * @since 3.6.0
+ */
+public class Phase implements LifecyclePhase
+{
+    private final String value;
+    
+    public Phase( String value )
+    {
+        this.value = value;
+    }
+
+    @Override
+    public Phase resolve( String phase )
+    {
+        return this;
+    }
+    
+    public String getValue()
+    {
+        return value;
+    }
+
+    @Override
+    public int hashCode()
+    {
+        return Objects.hashCode( value );
+    }
+
+    @Override
+    public boolean equals( Object obj )
+    {
+        if ( this == obj )
+        {
+            return true;
+        }
+        if ( obj == null )
+        {
+            return false;
+
+        }
+        if ( getClass() != obj.getClass() )
+        {
+            return false;
+        }
+        Phase other = (Phase) obj;
+
+        return Objects.equals( this.value, other.value );
+    }
+}
diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/PhasesWrapper.java b/maven-core/src/main/java/org/apache/maven/lifecycle/PhasesWrapper.java
new file mode 100644
index 0000000..e506a05
--- /dev/null
+++ b/maven-core/src/main/java/org/apache/maven/lifecycle/PhasesWrapper.java
@@ -0,0 +1,49 @@
+package org.apache.maven.lifecycle;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Wraps a list of LifecyclePhases
+ * 
+ * @author Robert Scholte
+ * @since 3.6.0
+ */
+public class PhasesWrapper
+{
+    private List<LifecyclePhase> phases = new ArrayList<>();
+    
+    void setPhase( String phase )
+    {
+        phases.add( new Phase( phase ) );
+    }
+    
+    void setChoice( Choice choice )
+    {
+        phases.add( choice );
+    }
+    
+    List<LifecyclePhase> getPhases()
+    {
+        return phases;
+    }
+}
diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultLifecycleMappingDelegate.java b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultLifecycleMappingDelegate.java
index 1ddee05..bec182a 100644
--- a/maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultLifecycleMappingDelegate.java
+++ b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultLifecycleMappingDelegate.java
@@ -28,6 +28,8 @@ import java.util.TreeMap;
 import org.apache.maven.execution.MavenSession;
 import org.apache.maven.lifecycle.Lifecycle;
 import org.apache.maven.lifecycle.LifecycleMappingDelegate;
+import org.apache.maven.lifecycle.LifecycleResolver;
+import org.apache.maven.lifecycle.Phase;
 import org.apache.maven.model.Plugin;
 import org.apache.maven.model.PluginExecution;
 import org.apache.maven.plugin.BuildPluginManager;
@@ -55,6 +57,9 @@ public class DefaultLifecycleMappingDelegate
 
     @Requirement
     private BuildPluginManager pluginManager;
+    
+    @Requirement
+    private LifecycleResolver lifecycleResolver;
 
     public Map<String, List<MojoExecution>> calculateLifecycleMappings( MavenSession session, MavenProject project,
                                                                         Lifecycle lifecycle, String lifecyclePhase )
@@ -66,19 +71,13 @@ public class DefaultLifecycleMappingDelegate
          * is interested in, i.e. all phases up to and including the specified phase.
          */
 
-        Map<String, Map<Integer, List<MojoExecution>>> mappings =
-            new LinkedHashMap<>();
+        Map<String, Map<Integer, List<MojoExecution>>> mappings = new LinkedHashMap<>();
 
-        for ( String phase : lifecycle.getPhases() )
+        for ( Phase phase : lifecycleResolver.resolve( lifecycle, lifecyclePhase ) )
         {
             Map<Integer, List<MojoExecution>> phaseBindings = new TreeMap<>();
 
-            mappings.put( phase, phaseBindings );
-
-            if ( phase.equals( lifecyclePhase ) )
-            {
-                break;
-            }
+            mappings.put( phase.getValue(), phaseBindings );
         }
 
         /*
diff --git a/maven-core/src/main/resources/META-INF/plexus/components.xml b/maven-core/src/main/resources/META-INF/plexus/components.xml
index 3f099cb..006edac 100644
--- a/maven-core/src/main/resources/META-INF/plexus/components.xml
+++ b/maven-core/src/main/resources/META-INF/plexus/components.xml
@@ -56,8 +56,12 @@ under the License.
           <phase>integration-test</phase>
           <phase>post-integration-test</phase>
           <phase>verify</phase>
-          <phase>install</phase>
-          <phase>deploy</phase>
+          <choice>
+            <phases>
+              <phase>install</phase>
+              <phase>deploy</phase>
+            </phases>
+          </choice>
         </phases>
         <!-- END SNIPPET: lifecycle -->
       </configuration>
diff --git a/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleResolverTest.java b/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleResolverTest.java
new file mode 100644
index 0000000..e34a12f
--- /dev/null
+++ b/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleResolverTest.java
@@ -0,0 +1,59 @@
+package org.apache.maven.lifecycle;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.List;
+
+import org.codehaus.plexus.PlexusTestCase;
+
+public class LifecycleResolverTest extends PlexusTestCase
+{
+    private LifecycleResolver resolver;
+    
+    private DefaultLifecycles lifecycles;
+
+    @Override
+    protected void setUp()
+        throws Exception
+    {
+        super.setUp();
+        this.resolver = lookup( LifecycleResolver.class );
+        this.lifecycles = lookup( DefaultLifecycles.class );
+    }
+    
+    public void testResolveChoiceInstall()
+    {
+        Lifecycle lifecycle = lifecycles.get( "install" );
+        List<Phase> phases = resolver.resolve( lifecycle, "install" );
+        
+        assertTrue( phases.contains( new Phase( "install" ) ) );
+        assertFalse( phases.contains( new Phase( "deploy" ) ) );
+    }
+
+    public void testResolveChoiceDeploy()
+    {
+        Lifecycle lifecycle = lifecycles.get( "deploy" );
+        List<Phase> phases = resolver.resolve( lifecycle, "deploy" );
+        
+        assertFalse( phases.contains( new Phase( "install" ) ) );
+        assertTrue( phases.contains( new Phase( "deploy" ) ) );
+    }
+
+}
diff --git a/maven-core/src/test/java/org/apache/maven/lifecycle/internal/stub/DefaultLifecyclesStub.java b/maven-core/src/test/java/org/apache/maven/lifecycle/internal/stub/DefaultLifecyclesStub.java
index 1dc2b6b..15ee889 100644
--- a/maven-core/src/test/java/org/apache/maven/lifecycle/internal/stub/DefaultLifecyclesStub.java
+++ b/maven-core/src/test/java/org/apache/maven/lifecycle/internal/stub/DefaultLifecyclesStub.java
@@ -17,6 +17,8 @@ package org.apache.maven.lifecycle.internal.stub;
 
 import org.apache.maven.lifecycle.DefaultLifecycles;
 import org.apache.maven.lifecycle.Lifecycle;
+import org.apache.maven.lifecycle.LifecyclePhase;
+import org.apache.maven.lifecycle.Phase;
 
 import java.util.Arrays;
 import java.util.HashMap;
@@ -35,20 +37,31 @@ public class DefaultLifecyclesStub
     public static DefaultLifecycles createDefaultLifecycles()
     {
 
-        List<String> stubDefaultCycle =
-            Arrays.asList( VALIDATE.getPhase(), INITIALIZE.getPhase(), PROCESS_RESOURCES.getPhase(), COMPILE.getPhase(),
-                           TEST.getPhase(), PROCESS_TEST_RESOURCES.getPhase(), PACKAGE.getPhase(), "BEER",
-                           INSTALL.getPhase() );
+        List<LifecyclePhase> stubDefaultCycle =
+            Arrays.<LifecyclePhase>asList( new Phase( VALIDATE.getPhase() ),
+                                           new Phase( INITIALIZE.getPhase() ),
+                                           new Phase( PROCESS_RESOURCES.getPhase() ),
+                                           new Phase( COMPILE.getPhase() ),
+                                           new Phase( TEST.getPhase() ),
+                                           new Phase( PROCESS_TEST_RESOURCES.getPhase() ),
+                                           new Phase( PACKAGE.getPhase() ),
+                                           new Phase( "BEER" ),
+                                           new Phase( INSTALL.getPhase() ) );
 
         // The two phases below are really for future expansion, some would say they lack a drink
         // The point being that they do not really have to match the "real" stuff,
-        List<String> stubCleanCycle = Arrays.asList( PRE_CLEAN.getPhase(), CLEAN.getPhase(), POST_CLEAN.getPhase() );
+        List<LifecyclePhase> stubCleanCycle =
+            Arrays.<LifecyclePhase>asList( new Phase( PRE_CLEAN.getPhase() ),
+                                           new Phase( CLEAN.getPhase() ), 
+                                           new Phase( POST_CLEAN.getPhase() ) );
 
-        List<String> stubSiteCycle =
-            Arrays.asList( PRE_SITE.getPhase(), SITE.getPhase(), POST_SITE.getPhase(), SITE_DEPLOY.getPhase() );
+        List<LifecyclePhase> stubSiteCycle =
+            Arrays.<LifecyclePhase>asList( new Phase( PRE_SITE.getPhase() ),
+                                           new Phase( SITE.getPhase() ),
+                                           new Phase( POST_SITE.getPhase() ),
+                                           new Phase( SITE_DEPLOY.getPhase() ) );
 
-        @SuppressWarnings( "unchecked" )
-        Iterator<List<String>> lcs = Arrays.asList( stubDefaultCycle, stubCleanCycle, stubSiteCycle ).iterator();
+        Iterator<List<LifecyclePhase>> lcs = Arrays.asList( stubDefaultCycle, stubCleanCycle, stubSiteCycle ).iterator();
 
         Map<String, Lifecycle> lifeCycles = new HashMap<>();
         for ( String s : DefaultLifecycles.STANDARD_LIFECYCLES )