You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by kw...@apache.org on 2018/01/03 10:13:50 UTC

[sling-slingstart-maven-plugin] 01/01: SLING-7334 only consider Maven modules leveraging the same version of the s-m-p in the DependencyLifecycleParticipant

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

kwin pushed a commit to branch bugfix/SLING-7334-separate-dependency-lifecycle-participant
in repository https://gitbox.apache.org/repos/asf/sling-slingstart-maven-plugin.git

commit 9f5d76a8f11f08ea1cc30566b3c4ff4d31e7a925
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Wed Dec 27 19:59:56 2017 +0100

    SLING-7334 only consider Maven modules leveraging the same version of
    the s-m-p in the DependencyLifecycleParticipant
---
 pom.xml                                            | 18 ++++++++
 .../slingstart/DependencyLifecycleParticipant.java | 53 +++++++++++++++++++---
 .../DependencyLifecycleParticipantIT.java          | 35 ++++++++++++++
 3 files changed, 99 insertions(+), 7 deletions(-)

diff --git a/pom.xml b/pom.xml
index 1910445..8150f56 100644
--- a/pom.xml
+++ b/pom.xml
@@ -127,6 +127,24 @@
                     </excludes>
                 </configuration>
             </plugin>
+            <plugin>
+                <artifactId>maven-failsafe-plugin</artifactId>
+                <!-- for https://issues.apache.org/jira/browse/SUREFIRE-855 -->
+                <version>2.20.1</version>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>integration-test</goal>
+                            <goal>verify</goal>
+                        </goals>
+                    </execution>
+                </executions>
+                <configuration>
+                    <systemPropertyVariables>
+                        <project.version>${project.version}</project.version>
+                    </systemPropertyVariables>
+                </configuration>
+            </plugin>
         </plugins>
     </build>
 
diff --git a/src/main/java/org/apache/sling/maven/slingstart/DependencyLifecycleParticipant.java b/src/main/java/org/apache/sling/maven/slingstart/DependencyLifecycleParticipant.java
index 7335f9f..cd83d4d 100644
--- a/src/main/java/org/apache/sling/maven/slingstart/DependencyLifecycleParticipant.java
+++ b/src/main/java/org/apache/sling/maven/slingstart/DependencyLifecycleParticipant.java
@@ -16,6 +16,10 @@
  */
 package org.apache.sling.maven.slingstart;
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Properties;
+
 import org.apache.maven.AbstractMavenLifecycleParticipant;
 import org.apache.maven.MavenExecutionException;
 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
@@ -31,14 +35,22 @@ import org.codehaus.plexus.logging.Logger;
 
 /**
  * Maven lifecycle participant which adds the artifacts of the model to the dependencies.
+ * This cannot happen as part of a regular Mojo (as there the dependencies have already been calculated)
+ * therefore a build extension is necessary to achieve that.
+ * This build extension is loaded once per version of the slingstart-maven-plugin being referenced in any of the modules.
+ * @see <a href="https://issues.apache.org/jira/browse/MNG-4224">MNG-4224 - Maven Lifecycle Participant</a>
+ * @see <a href="http://takari.io/book/91-maven-classloading.html#plugin-classloaders">Maven Classloading</a>
  */
 @Component(role = AbstractMavenLifecycleParticipant.class)
 public class DependencyLifecycleParticipant extends AbstractMavenLifecycleParticipant {
 
+    private static final String GROUP_ID = "org.apache.sling";
+    private static final String ARTIFACT_ID = "slingstart-maven-plugin";
+    
     /**
      *  the plugin ID consists of <code>groupId:artifactId</code>, see {@link Plugin#constructKey(String, String)}
      */
-    private static final String PLUGIN_ID = "org.apache.sling:slingstart-maven-plugin";
+    private static final String PLUGIN_ID = GROUP_ID + ":" + ARTIFACT_ID;
 
     @Requirement
     private Logger logger;
@@ -61,20 +73,47 @@ public class DependencyLifecycleParticipant extends AbstractMavenLifecyclePartic
         env.logger = logger;
         env.session = session;
 
-        logger.debug("Searching for project leveraging plugin '" + PLUGIN_ID + "'...");
+        final String version;
+        try {
+            version = getCurrentPluginVersion();
+        } catch (IOException e) {
+            throw new MavenExecutionException("Could not retrieve extensions version", e);
+        }
+        logger.debug("Searching for projects leveraging plugin '" + PLUGIN_ID + "' in version "+ version + "...");
 
         for (final MavenProject project : session.getProjects()) {
             // consider all projects where this plugin is configured
             Plugin plugin = project.getPlugin(PLUGIN_ID);
             if (plugin != null) {
-                logger.debug("Found project " + project + " leveraging " + PLUGIN_ID +".");
-                final ProjectInfo info = new ProjectInfo();
-                info.plugin = plugin;
-                info.project = project;
-                env.modelProjects.put(project.getGroupId() + ":" + project.getArtifactId(), info);
+                if (version.equals(plugin.getVersion())) {
+                    logger.debug("Found project " + project + " leveraging " + PLUGIN_ID +" in version "+ version + ".");
+                    final ProjectInfo info = new ProjectInfo();
+                    info.plugin = plugin;
+                    info.project = project;
+                    env.modelProjects.put(project.getGroupId() + ":" + project.getArtifactId(), info);
+                } else {
+                    logger.debug("Skipping project " + project + " leveraging " + PLUGIN_ID +" in another version "+ project.getVersion() + ".");
+                }
             }
         }
 
         new ModelPreprocessor().addDependencies(env);
     }
+    
+    /**
+     * Retrieves the version of the encapsulating Mojo by evaluating the {@code pom.properties} loaded via the extension classloader
+     * @throws IOException 
+     * @see <a href="https://maven.apache.org/shared/maven-archiver/#pom-properties-content">Maven Archiver - pom.properties</a>
+     */
+    static final String getCurrentPluginVersion() throws IOException {
+        final String pomPropertiesFile = String.format("/META-INF/maven/%s/%s/pom.properties", GROUP_ID, ARTIFACT_ID);
+        try (InputStream inputStream = DependencyLifecycleParticipant.class.getResourceAsStream(pomPropertiesFile)) {
+            if (inputStream == null) {
+                throw new IllegalStateException("Could not find '" + pomPropertiesFile + "' via classloader '" + DependencyLifecycleParticipant.class.getClassLoader() + "'");
+            }
+            final Properties properties = new Properties();
+            properties.load(inputStream);
+            return properties.getProperty("version");
+        }
+    }
 }
diff --git a/src/test/java/org/apache/sling/maven/slingstart/DependencyLifecycleParticipantIT.java b/src/test/java/org/apache/sling/maven/slingstart/DependencyLifecycleParticipantIT.java
new file mode 100644
index 0000000..0e4dc02
--- /dev/null
+++ b/src/test/java/org/apache/sling/maven/slingstart/DependencyLifecycleParticipantIT.java
@@ -0,0 +1,35 @@
+/*
+ * 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.
+ */
+package org.apache.sling.maven.slingstart;
+
+import java.io.IOException;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+
+public class DependencyLifecycleParticipantIT {
+
+    private static final String PROPERTY_PROJECT_VERSION = "project.version";
+
+    @Test
+    public void testGetCurrentPluginVersion() throws IOException {
+        String version = System.getProperty(PROPERTY_PROJECT_VERSION);
+        Assert.assertNotNull("This test must be called with property " + PROPERTY_PROJECT_VERSION + " being set to the project version", version);
+        Assert.assertEquals(version, DependencyLifecycleParticipant.getCurrentPluginVersion());
+    }
+}

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.