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 2020/06/08 21:37:28 UTC

[maven-ear-plugin] branch master updated: [MEAR-278] Ear plugin includes the same artifact twice if used without clean

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

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


The following commit(s) were added to refs/heads/master by this push:
     new df53696  [MEAR-278] Ear plugin includes the same artifact twice if used without clean
df53696 is described below

commit df5369606722bc4d154164e952fe8890b5644120
Author: rfscholte <rf...@apache.org>
AuthorDate: Mon Jun 8 23:01:28 2020 +0200

    [MEAR-278] Ear plugin includes the same artifact twice if used without clean
---
 .../invoker.properties                             | 25 +++++++
 src/it/MEAR-278-same-artifact-twice/pom.xml        | 48 +++++++++++++
 src/it/MEAR-278-same-artifact-twice/pom1.xml       | 58 ++++++++++++++++
 src/it/MEAR-278-same-artifact-twice/pom2.xml       | 58 ++++++++++++++++
 src/it/MEAR-278-same-artifact-twice/verify.groovy  | 42 ++++++++++++
 .../java/org/apache/maven/plugins/ear/EarMojo.java | 79 ++++++++++++++++++++--
 6 files changed, 304 insertions(+), 6 deletions(-)

diff --git a/src/it/MEAR-278-same-artifact-twice/invoker.properties b/src/it/MEAR-278-same-artifact-twice/invoker.properties
new file mode 100644
index 0000000..6636c52
--- /dev/null
+++ b/src/it/MEAR-278-same-artifact-twice/invoker.properties
@@ -0,0 +1,25 @@
+# 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.
+
+invoker.project.1 = pom1.xml
+
+invoker.goals.1 = package
+
+invoker.project.2 = pom2.xml
+
+invoker.goals.2 = package
+
diff --git a/src/it/MEAR-278-same-artifact-twice/pom.xml b/src/it/MEAR-278-same-artifact-twice/pom.xml
new file mode 100644
index 0000000..3381f8e
--- /dev/null
+++ b/src/it/MEAR-278-same-artifact-twice/pom.xml
@@ -0,0 +1,48 @@
+<?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/xsd/maven-4.0.0.xsd">
+	<modelVersion>4.0.0</modelVersion>
+
+	<groupId>de.fabianmeier</groupId>
+	<artifactId>test-ear-parent</artifactId>
+	<version>0.0.1-SNAPSHOT</version>
+	<packaging>pom</packaging>
+
+	<name>test-ear</name>
+	<url>http://maven.apache.org</url>
+
+	<properties>
+		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+	</properties>
+
+	<build>
+		<plugins>
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-ear-plugin</artifactId>
+				<version>@project.version@</version>
+			</plugin>
+		</plugins>
+	</build>
+</project>
diff --git a/src/it/MEAR-278-same-artifact-twice/pom1.xml b/src/it/MEAR-278-same-artifact-twice/pom1.xml
new file mode 100644
index 0000000..3c69f64
--- /dev/null
+++ b/src/it/MEAR-278-same-artifact-twice/pom1.xml
@@ -0,0 +1,58 @@
+<?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/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+       <groupId>de.fabianmeier</groupId>
+        <artifactId>test-ear-parent</artifactId>
+        <version>0.0.1-SNAPSHOT</version>
+        <relativePath>pom.xml</relativePath>
+    </parent>
+
+    <groupId>de.fabianmeier</groupId>
+    <artifactId>test-ear</artifactId>
+    <version>0.0.1-SNAPSHOT</version>
+    <packaging>ear</packaging>
+
+    <name>test-ear</name>
+    <url>http://maven.apache.org</url>
+
+    <properties>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>log4j</groupId>
+            <artifactId>log4j</artifactId>
+            <version>1.2.8</version>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.12</version>
+        </dependency>
+    </dependencies>
+</project>
diff --git a/src/it/MEAR-278-same-artifact-twice/pom2.xml b/src/it/MEAR-278-same-artifact-twice/pom2.xml
new file mode 100644
index 0000000..e77b5f0
--- /dev/null
+++ b/src/it/MEAR-278-same-artifact-twice/pom2.xml
@@ -0,0 +1,58 @@
+<?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/xsd/maven-4.0.0.xsd">
+	<modelVersion>4.0.0</modelVersion>
+
+	<parent>
+		<groupId>de.fabianmeier</groupId>
+		<artifactId>test-ear-parent</artifactId>
+		<version>0.0.1-SNAPSHOT</version>
+		<relativePath>pom.xml</relativePath>
+	</parent>
+
+	<groupId>de.fabianmeier</groupId>
+	<artifactId>test-ear</artifactId>
+	<version>0.0.1-SNAPSHOT</version>
+	<packaging>ear</packaging>
+
+	<name>test-ear</name>
+	<url>http://maven.apache.org</url>
+
+	<properties>
+		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+	</properties>
+
+	<dependencies>
+		<dependency>
+			<groupId>log4j</groupId>
+			<artifactId>log4j</artifactId>
+			<version>1.2.17</version>
+		</dependency>
+		<dependency>
+			<groupId>junit</groupId>
+			<artifactId>junit</artifactId>
+			<version>4.12</version>
+		</dependency>
+	</dependencies>
+</project>
diff --git a/src/it/MEAR-278-same-artifact-twice/verify.groovy b/src/it/MEAR-278-same-artifact-twice/verify.groovy
new file mode 100644
index 0000000..1707947
--- /dev/null
+++ b/src/it/MEAR-278-same-artifact-twice/verify.groovy
@@ -0,0 +1,42 @@
+/*
+ * 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.jar.JarFile
+
+File jarFile = new File( basedir, "target/test-ear-0.0.1-SNAPSHOT.ear" )
+
+JarFile jar = new JarFile( jarFile )
+
+def shouldNotBeIncludedEntries = [ "log4j-log4j-1.2.8.jar" ]
+
+println shouldNotBeIncludedEntries
+
+for ( String entry : shouldNotBeIncludedEntries )
+{
+    print( "Checking for existence of " + entry )
+
+    if ( jar.getEntry( entry ) == null )
+    {
+       return true
+    }
+}
+
+jar.close()
+
+return false
diff --git a/src/main/java/org/apache/maven/plugins/ear/EarMojo.java b/src/main/java/org/apache/maven/plugins/ear/EarMojo.java
index b22cb5f..413f3b6 100644
--- a/src/main/java/org/apache/maven/plugins/ear/EarMojo.java
+++ b/src/main/java/org/apache/maven/plugins/ear/EarMojo.java
@@ -23,8 +23,16 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.nio.file.FileVisitResult;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.SimpleFileVisitor;
+import java.nio.file.attribute.BasicFileAttributes;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
 import java.util.List;
 import java.util.zip.ZipException;
 
@@ -57,6 +65,7 @@ import org.codehaus.plexus.archiver.manager.ArchiverManager;
 import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
 import org.codehaus.plexus.archiver.zip.ZipArchiver;
 import org.codehaus.plexus.archiver.zip.ZipUnArchiver;
+import org.codehaus.plexus.components.io.filemappers.FileMapper;
 import org.codehaus.plexus.util.DirectoryScanner;
 import org.codehaus.plexus.util.StringUtils;
 
@@ -261,7 +270,7 @@ public class EarMojo
     private MavenSession session;
 
     private List<FileUtils.FilterWrapper> filterWrappers;
-
+    
     /**
      * @since 2.9
      */
@@ -279,17 +288,46 @@ public class EarMojo
         zipUnArchiver.setUseJvmChmod( useJvmChmod );
 
         final JavaEEVersion javaEEVersion = JavaEEVersion.getJavaEEVersion( version );
+        
+        final Collection<String> outdatedResources;
+        
+        if ( !getWorkDirectory().exists() )
+        {
+            outdatedResources = Collections.emptyList(); 
+        }
+        else
+        {
+            outdatedResources = new ArrayList<>();
+            try
+            {
+                Files.walkFileTree( getWorkDirectory().toPath(), new SimpleFileVisitor<Path>() 
+                {
+                    @Override
+                    public FileVisitResult visitFile( Path file, BasicFileAttributes attrs )
+                        throws IOException
+                    {
+                        outdatedResources.add( getWorkDirectory().toPath().relativize( file ).toString() );
+                        return super.visitFile( file, attrs );
+                    }
+                } );
+            }
+            catch ( IOException e )
+            {
+                getLog().warn( "Can't detect outdated resources", e );
+            } 
+        }
 
         // Initializes unpack types
         List<String> unpackTypesList = createUnpackList();
 
         // Copy modules
-        copyModules( javaEEVersion, unpackTypesList );
+        copyModules( javaEEVersion, unpackTypesList, outdatedResources );
 
         // Copy source files
         try
         {
             File earSourceDir = earSourceDirectory;
+            
             if ( earSourceDir.exists() )
             {
                 getLog().info( "Copy ear sources to " + getWorkDirectory().getAbsolutePath() );
@@ -297,6 +335,7 @@ public class EarMojo
                 for ( String fileName : fileNames )
                 {
                     copyFile( new File( earSourceDir, fileName ), new File( getWorkDirectory(), fileName ) );
+                    outdatedResources.remove( Paths.get( fileName ).toString() );
                 }
             }
 
@@ -306,8 +345,8 @@ public class EarMojo
                 getLog().info( "Including custom application.xml[" + applicationXml + "]" );
                 File metaInfDir = new File( getWorkDirectory(), META_INF );
                 copyFile( new File( applicationXml ), new File( metaInfDir, "/application.xml" ) );
+                outdatedResources.remove( Paths.get( "META-INF/application.xml" ).toString() );
             }
-
         }
         catch ( IOException e )
         {
@@ -327,6 +366,17 @@ public class EarMojo
                 + " does not exist." );
             // CHECKSTYLE_ON: LineLength
         }
+        
+        final long startTime = session.getStartTime().getTime();
+        
+        // generate-application-xml writes directly to working directory, so needs to be verified based on timestamp
+        for ( String outdatedResource : outdatedResources )
+        {
+            if ( new File( getWorkDirectory(), outdatedResource ).lastModified() < startTime )
+            {
+                new File( getWorkDirectory(), outdatedResource ).delete();
+            }
+        }
 
         try
         {
@@ -363,9 +413,13 @@ public class EarMojo
         }
     }
 
-    private void copyModules( final JavaEEVersion javaEEVersion, List<String> unpackTypesList )
+    private void copyModules( final JavaEEVersion javaEEVersion, 
+                              List<String> unpackTypesList, 
+                              Collection<String> outdatedResources )
         throws MojoExecutionException, MojoFailureException
     {
+        final Path workingDir = getWorkDirectory().toPath();
+        
         try
         {
             for ( EarModule module : getModules() )
@@ -397,7 +451,7 @@ public class EarMojo
                     getLog().info( "Copying artifact [" + module + "] to [" + module.getUri() + "] (unpacked)" );
                     // Make sure that the destination is a directory to avoid plexus nasty stuff :)
                     destinationFile.mkdirs();
-                    unpack( sourceFile, destinationFile );
+                    unpack( sourceFile, destinationFile, outdatedResources );
 
                     if ( skinnyWars && module.changeManifestClasspath() )
                     {
@@ -421,6 +475,7 @@ public class EarMojo
                         getLog().debug( "Skipping artifact [" + module + "], as it is already up to date at ["
                             + module.getUri() + "]" );
                     }
+                    outdatedResources.remove( workingDir.relativize( destinationFile.toPath() ).toString() );
                 }
             }
         }
@@ -607,12 +662,24 @@ public class EarMojo
      * @throws NoSuchArchiverException In case of we don't have an appropriate archiver.
      * @throws IOException In case of a general IOException.
      */
-    public void unpack( File source, File destDir )
+    public void unpack( File source, final File destDir, final Collection<String> outdatedResources )
         throws NoSuchArchiverException, IOException
     {
         UnArchiver unArchiver = archiverManager.getUnArchiver( "zip" );
         unArchiver.setSourceFile( source );
         unArchiver.setDestDirectory( destDir );
+        unArchiver.setFileMappers( new FileMapper[] {
+            new FileMapper()
+            {
+                @Override
+                public String getMappedFileName( String pName )
+                {
+                    Path destFile = destDir.toPath().resolve( pName );
+                    outdatedResources.remove( getWorkDirectory().toPath().relativize( destFile ).toString() );
+                    return pName;
+                }
+            }
+        } );
 
         // Extract the module
         unArchiver.extract();