From f6665aaddd13f0cd16746598e229bc383686fecb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ramiro=20Pereira=20de=20Magalh=C3=A3es?= Date: Fri, 25 Nov 2016 13:14:48 -0200 Subject: [PATCH 1/2] First proposal to resolve issue #51. --- .../AbstractAppAssemblerMojo.java | 34 ++++++++++++++----- .../AbstractScriptGeneratorMojo.java | 2 +- .../appassembler/CreateRepositoryMojo.java | 4 +-- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/appassembler-maven-plugin/src/main/java/org/codehaus/mojo/appassembler/AbstractAppAssemblerMojo.java b/appassembler-maven-plugin/src/main/java/org/codehaus/mojo/appassembler/AbstractAppAssemblerMojo.java index ab14bfe5..b290f88c 100644 --- a/appassembler-maven-plugin/src/main/java/org/codehaus/mojo/appassembler/AbstractAppAssemblerMojo.java +++ b/appassembler-maven-plugin/src/main/java/org/codehaus/mojo/appassembler/AbstractAppAssemblerMojo.java @@ -26,6 +26,7 @@ import java.io.File; import java.io.IOException; +import java.nio.file.Files; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.repository.ArtifactRepository; @@ -97,6 +98,15 @@ public abstract class AbstractAppAssemblerMojo @Parameter( defaultValue = "false" ) protected boolean preClean; + /** + * If set to {@code true}, overwrites existing dependencies in the repository. If set to {@code false}, + * it will check if the dependency is already present in the repository prior copying to it. + * + * @since 2.0.1 + */ + @Parameter( defaultValue = "true" ) + protected boolean overwriteDependencies; + // ----------------------------------------------------------------------- // Read-only parameters // ----------------------------------------------------------------------- @@ -169,10 +179,11 @@ protected ArtifactRepositoryLayout getArtifactRepositoryLayout() * @param artifact The artifact to install. * @param artifactRepository The repository where to install. * @param useTimestampInSnapshotFileName Using timestamp for SNAPSHOT's. + * @param overwriteExistingArtifacts Set to {@code false} to avoid overwriting existing artifacts. * @throws MojoExecutionException */ protected void installArtifact( Artifact artifact, ArtifactRepository artifactRepository, - boolean useTimestampInSnapshotFileName ) + boolean useTimestampInSnapshotFileName, boolean overwriteExistingArtifacts ) throws MojoExecutionException { if ( artifact != null && artifact.getFile() != null ) @@ -206,13 +217,20 @@ protected void installArtifact( Artifact artifact, ArtifactRepository artifactRe String fileName = MappingUtils.evaluateFileNameMapping( outputFileNameMapping, artifact ); destination = new File( destination.getParent(), fileName ); } - // Sometimes target/classes is in the artifact list and copyFile() would fail. - // Need to ignore this condition - FileUtils.copyFile( source, destination ); - } - - getLog().info( "Installing artifact " + source.getPath() + " to " + destination ); + if ( overwriteExistingArtifacts ) + { + // Sometimes target/classes is in the artifact list and copyFile() would fail. + // Need to ignore this condition + FileUtils.copyFile( source, destination ); + getLog().info( "Installing artifact " + source.getPath() + " to " + destination ); + } + else if ( Files.exists( destination.toPath() ) ) + { + FileUtils.copyFile( source, destination ); + getLog().info( "Skiped installing artifact " + source.getPath() + " to " + destination + ". File already exists."); + } + } } catch ( IOException e ) { @@ -235,7 +253,7 @@ protected void installArtifact( Artifact artifact, ArtifactRepository artifactRe protected void installArtifact( Artifact artifact, ArtifactRepository artifactRepository ) throws MojoExecutionException { - installArtifact( artifact, artifactRepository, true ); + installArtifact( artifact, artifactRepository, true, true ); } diff --git a/appassembler-maven-plugin/src/main/java/org/codehaus/mojo/appassembler/AbstractScriptGeneratorMojo.java b/appassembler-maven-plugin/src/main/java/org/codehaus/mojo/appassembler/AbstractScriptGeneratorMojo.java index b92841c6..a4596a68 100644 --- a/appassembler-maven-plugin/src/main/java/org/codehaus/mojo/appassembler/AbstractScriptGeneratorMojo.java +++ b/appassembler-maven-plugin/src/main/java/org/codehaus/mojo/appassembler/AbstractScriptGeneratorMojo.java @@ -373,7 +373,7 @@ protected void installDependencies( final String outputDirectory, final String r for ( Artifact artifact : artifacts ) { - installArtifact( artifact, artifactRepository, this.useTimestampInSnapshotFileName ); + installArtifact( artifact, artifactRepository, this.useTimestampInSnapshotFileName, true ); } // install the project's artifact in the new repository diff --git a/appassembler-maven-plugin/src/main/java/org/codehaus/mojo/appassembler/CreateRepositoryMojo.java b/appassembler-maven-plugin/src/main/java/org/codehaus/mojo/appassembler/CreateRepositoryMojo.java index 6c4c1c59..290cfc86 100644 --- a/appassembler-maven-plugin/src/main/java/org/codehaus/mojo/appassembler/CreateRepositoryMojo.java +++ b/appassembler-maven-plugin/src/main/java/org/codehaus/mojo/appassembler/CreateRepositoryMojo.java @@ -157,7 +157,7 @@ public void execute() // TODO: merge with the artifacts below so no duplicate versions included for ( Artifact artifact : artifacts ) { - installArtifact( artifact, artifactRepository, this.useTimestampInSnapshotFileName ); + installArtifact( artifact, artifactRepository, this.useTimestampInSnapshotFileName, overwriteDependencies ); } if ( installBooterArtifacts ) @@ -189,7 +189,7 @@ private void installBooterArtifacts( ArtifactRepository artifactRepository ) for ( Iterator i = result.getArtifacts().iterator(); i.hasNext(); ) { Artifact a = (Artifact) i.next(); - installArtifact( a, artifactRepository, this.useTimestampInSnapshotFileName ); + installArtifact( a, artifactRepository, this.useTimestampInSnapshotFileName, true ); } } catch ( ArtifactResolutionException e ) From 03417bac5e1ae01b4a512908214ec232eb9dfd4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ramiro=20Pereira=20de=20Magalh=C3=A3es?= Date: Fri, 25 Nov 2016 16:42:39 -0200 Subject: [PATCH 2/2] Fixing wrong if. Touching the installed file. --- .../appassembler/AbstractAppAssemblerMojo.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/appassembler-maven-plugin/src/main/java/org/codehaus/mojo/appassembler/AbstractAppAssemblerMojo.java b/appassembler-maven-plugin/src/main/java/org/codehaus/mojo/appassembler/AbstractAppAssemblerMojo.java index b290f88c..07aae87a 100644 --- a/appassembler-maven-plugin/src/main/java/org/codehaus/mojo/appassembler/AbstractAppAssemblerMojo.java +++ b/appassembler-maven-plugin/src/main/java/org/codehaus/mojo/appassembler/AbstractAppAssemblerMojo.java @@ -225,10 +225,18 @@ protected void installArtifact( Artifact artifact, ArtifactRepository artifactRe FileUtils.copyFile( source, destination ); getLog().info( "Installing artifact " + source.getPath() + " to " + destination ); } - else if ( Files.exists( destination.toPath() ) ) + else { - FileUtils.copyFile( source, destination ); - getLog().info( "Skiped installing artifact " + source.getPath() + " to " + destination + ". File already exists."); + if ( !Files.exists( destination.toPath() ) ) + { + FileUtils.copyFile( source, destination ); + } + else + { + destination.setLastModified(System.currentTimeMillis()); //'touches' the file + } + + getLog().info( "Skiped installing artifact " + source.getPath() + " to " + destination + ". File already exists."); } } }