You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ol...@apache.org on 2020/08/17 07:34:35 UTC

[maven-invoker-plugin] branch master updated: [MINVOKER-268] - Introduce updateOnly parameter to AbstractInvokerMojo (#24)

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

olamy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-invoker-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new 0196c36  [MINVOKER-268] - Introduce updateOnly parameter to AbstractInvokerMojo (#24)
0196c36 is described below

commit 0196c3676e8bf5020b39379ccc36ed682886e30f
Author: Rob Oxspring <ro...@imapmail.org>
AuthorDate: Mon Aug 17 08:34:28 2020 +0100

    [MINVOKER-268] - Introduce updateOnly parameter to AbstractInvokerMojo (#24)
    
    * [MINVOKER-268] - Refactor to avoid duplicate calls to cloneProjects()
    
    * [MINVOKER-268] - Introduce updateOnly parameter to AbstractInvokerMojo
---
 src/it/updateOnly/pom.xml                          | 90 ++++++++++++++++++++++
 src/it/updateOnly/src/it/project/pom.xml           | 30 ++++++++
 src/it/updateOnly/verify.bsh                       | 34 ++++++++
 .../maven/plugins/invoker/AbstractInvokerMojo.java | 59 ++++++++++++--
 4 files changed, 208 insertions(+), 5 deletions(-)

diff --git a/src/it/updateOnly/pom.xml b/src/it/updateOnly/pom.xml
new file mode 100644
index 0000000..e388df9
--- /dev/null
+++ b/src/it/updateOnly/pom.xml
@@ -0,0 +1,90 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+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.
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.apache.maven.plugins.invoker</groupId>
+  <artifactId>updateOnly</artifactId>
+  <version>1.0-SNAPSHOT</version>
+  <packaging>pom</packaging>
+
+  <description>
+    Test to check that invocations can use a different POM than the one that was selected via the pomInclude
+    parameter. This allows fine-grained control over the build of a multi-module project where the modules are
+    (intentionally) not collectively build by the reactor.
+  </description>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-antrun-plugin</artifactId>
+        <version>3.0.0</version>
+        <executions>
+          <execution>
+            <phase>initialize</phase>
+            <goals>
+              <goal>run</goal>
+            </goals>
+            <configuration>
+              <target>
+                <!-- simulate recently modified file in target directory -->
+                <touch file="${project.build.directory}/it/project/target/marker.txt" mkdirs="true" />
+              </target>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-invoker-plugin</artifactId>
+        <version>@project.version@</version>
+        <configuration>
+          <cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>
+          <pomIncludes>
+            <pomInclude>project/pom.xml</pomInclude>
+          </pomIncludes>
+          <postBuildHookScript>verify</postBuildHookScript>
+          <goals>
+            <goal>package</goal>
+          </goals>
+          <!-- turn on the feature-under-test -->
+          <updateOnly>true</updateOnly>
+        </configuration>
+        <executions>
+          <execution>
+            <id>integration-test</id>
+            <phase>initialize</phase>
+            <goals>
+              <goal>run</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+
+</project>
diff --git a/src/it/updateOnly/src/it/project/pom.xml b/src/it/updateOnly/src/it/project/pom.xml
new file mode 100644
index 0000000..3d88840
--- /dev/null
+++ b/src/it/updateOnly/src/it/project/pom.xml
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+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.
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <packaging>jar</packaging>
+
+  <groupId>test</groupId>
+  <artifactId>project</artifactId>
+  <version>0.1-SNAPSHOT</version>
+
+</project>
diff --git a/src/it/updateOnly/verify.bsh b/src/it/updateOnly/verify.bsh
new file mode 100644
index 0000000..18922e7
--- /dev/null
+++ b/src/it/updateOnly/verify.bsh
@@ -0,0 +1,34 @@
+/*
+ * 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.io.*;
+
+File marker = new File( basedir, "target/it/project/target/marker.txt" );
+if ( !marker.exists() )
+{
+  throw new IOException( "Marker file should have been created in setup: " + marker );
+}
+
+File archive = new File( basedir, "target/it/project/target/project-0.1-SNAPSHOT.jar" );
+if ( archive.exists() )
+{
+  throw new IOException( "Target file creation should have been skipped: " + archive );
+}
+
+return true;
diff --git a/src/main/java/org/apache/maven/plugins/invoker/AbstractInvokerMojo.java b/src/main/java/org/apache/maven/plugins/invoker/AbstractInvokerMojo.java
index deec2cc..f6e3586 100644
--- a/src/main/java/org/apache/maven/plugins/invoker/AbstractInvokerMojo.java
+++ b/src/main/java/org/apache/maven/plugins/invoker/AbstractInvokerMojo.java
@@ -659,6 +659,15 @@ public abstract class AbstractInvokerMojo
     private String junitPackageName = "maven.invoker.it";
 
     /**
+     * Only invoke maven projects if their sources have been modified since
+     * they were last built. Only works in conjunction with <code>cloneProjectsTo</code>.
+
+     * @since 3.2.2
+     */
+    @Parameter( defaultValue = "false", property = "invoker.updateOnly" )
+    private boolean updateOnly = false;
+
+    /**
      * The scripter runner that is responsible to execute hook scripts.
      */
     private ScriptRunner scriptRunner;
@@ -754,14 +763,31 @@ public abstract class AbstractInvokerMojo
 
         File projectsDir = projectsDirectory;
 
-        if ( cloneProjectsTo != null )
+        if ( cloneProjectsTo == null && "maven-plugin".equals( project.getPackaging() ) )
         {
-            cloneProjects( collectedProjects );
-            projectsDir = cloneProjectsTo;
+            cloneProjectsTo = new File( project.getBuild().getDirectory(), "its" );
         }
-        else if ( cloneProjectsTo == null && "maven-plugin".equals( project.getPackaging() ) )
+
+        if ( updateOnly )
+        {
+            if ( cloneProjectsTo == null )
+            {
+                getLog().warn( "updateOnly functionality is not supported without cloning the projects" );
+            }
+            else if ( lastModifiedRecursive( projectsDirectory ) <= lastModifiedRecursive( cloneProjectsTo ) )
+            {
+                getLog().debug( "Skipping invocation as cloned projects are up-to-date"
+                        + "and updateOnly parameter is set to true." );
+                return;
+            }
+            else
+            {
+                getLog().debug( "Cloned projects are out of date" );
+            }
+        }
+
+        if ( cloneProjectsTo != null )
         {
-            cloneProjectsTo = new File( project.getBuild().getDirectory(), "its" );
             cloneProjects( collectedProjects );
             projectsDir = cloneProjectsTo;
         }
@@ -807,6 +833,29 @@ public abstract class AbstractInvokerMojo
     }
 
     /**
+     * Find the latest lastModified recursively within a directory structure.
+     *
+     * @param file the root file to check.
+     * @return the latest lastModified time found.
+     */
+    private long lastModifiedRecursive( File file )
+    {
+        long lastModified = file.lastModified();
+
+        final File[] entries = file.listFiles();
+
+        if ( entries != null )
+        {
+            for ( File entry : entries )
+            {
+                lastModified = Math.max( lastModified, lastModifiedRecursive( entry ) );
+            }
+        }
+
+        return lastModified;
+    }
+
+    /**
      * This will create the necessary folders for the reports.
      * 
      * @throws MojoExecutionException in case of failure during creation of the reports folder.