diff --git a/.gitignore b/.gitignore index 7495d7e2..3093e958 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ target/ +!src/test/resources/unit/*/target .project .classpath .settings/ diff --git a/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java b/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java index a006dace..a9eb098c 100644 --- a/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java +++ b/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java @@ -69,7 +69,7 @@ */ @Mojo( name = "install-file", requiresProject = false, aggregator = true, threadSafe = true ) public class InstallFileMojo - extends AbstractMojo + extends AbstractMojo { private static final String LS = System.getProperty( "line.separator" ); @@ -248,8 +248,8 @@ public void execute() ).setFile( file ); installRequest.addArtifact( mainArtifact ); - File artifactLocalFile = getLocalRepositoryFile( session.getRepositorySession(), mainArtifact ); - File pomLocalFile = getPomLocalRepositoryFile( session.getRepositorySession(), mainArtifact ); + File artifactLocalFile = getLocalRepositoryFile( repositorySystemSession, mainArtifact ); + File pomLocalFile = getPomLocalRepositoryFile( repositorySystemSession, mainArtifact ); if ( file.equals( artifactLocalFile ) ) { diff --git a/src/test/java/org/apache/maven/plugins/install/InstallFileMojoTest.java b/src/test/java/org/apache/maven/plugins/install/InstallFileMojoTest.java index 7efd90c0..bcfd69d7 100644 --- a/src/test/java/org/apache/maven/plugins/install/InstallFileMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/install/InstallFileMojoTest.java @@ -58,12 +58,79 @@ public class InstallFileMojoTest private final String LOCAL_REPO = "target/local-repo/"; + private final String SPECIFIC_LOCAL_REPO = "target/specific-local-repo/"; + public void setUp() throws Exception { super.setUp(); FileUtils.deleteDirectory( new File( getBasedir() + "/" + LOCAL_REPO ) ); + FileUtils.deleteDirectory( new File( getBasedir() + "/" + SPECIFIC_LOCAL_REPO ) ); + } + + public void testInstallFileFromLocalRepositoryToLocalRepositoryPath() + throws Exception + { + File localRepository = + new File( getBasedir(), "target/test-classes/unit/install-file-from-local-repository-test/target" ); + + File testPom = new File( localRepository.getParentFile(), "plugin-config.xml" ); + + InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom ); + + assertNotNull( mojo ); + + setVariableValueToObject( mojo, "session", createMavenSession( localRepository.getAbsolutePath() ) ); + + File specificLocalRepositoryPath = new File( getBasedir() + "/" + SPECIFIC_LOCAL_REPO ); + + setVariableValueToObject( mojo, "localRepositoryPath", specificLocalRepositoryPath ); + + assignValuesForParameter( mojo ); + + mojo.execute(); + + String localPath = getBasedir() + "/" + SPECIFIC_LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" + + artifactId + "-" + version; + + File installedArtifact = new File( localPath + "." + "jar" ); + + assertTrue( installedArtifact.exists() ); + + assertEquals( FileUtils.getFiles( new File( SPECIFIC_LOCAL_REPO ), null, null ).toString(), 5, + FileUtils.getFiles( new File( SPECIFIC_LOCAL_REPO ), null, null ).size() ); + } + + public void testInstallFileWithLocalRepositoryPath() + throws Exception + { + File testPom = + new File( getBasedir(), "target/test-classes/unit/install-file-with-checksum/" + "plugin-config.xml" ); + + InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom ); + + assertNotNull( mojo ); + + setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) ); + + File specificLocalRepositoryPath = new File( getBasedir() + "/" + SPECIFIC_LOCAL_REPO ); + + setVariableValueToObject( mojo, "localRepositoryPath", specificLocalRepositoryPath ); + + assignValuesForParameter( mojo ); + + mojo.execute(); + + String localPath = getBasedir() + "/" + SPECIFIC_LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" + + artifactId + "-" + version; + + File installedArtifact = new File( localPath + "." + "jar" ); + + assertTrue( installedArtifact.exists() ); + + assertEquals( FileUtils.getFiles( new File( SPECIFIC_LOCAL_REPO ), null, null ).toString(), 5, + FileUtils.getFiles( new File( SPECIFIC_LOCAL_REPO ), null, null ).size() ); } public void testInstallFileTestEnvironment() @@ -73,7 +140,7 @@ public void testInstallFileTestEnvironment() InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom ); - setVariableValueToObject( mojo, "session", createMavenSession() ); + setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) ); assertNotNull( mojo ); } @@ -87,7 +154,7 @@ public void testBasicInstallFile() assertNotNull( mojo ); - setVariableValueToObject( mojo, "session", createMavenSession() ); + setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) ); assignValuesForParameter( mojo ); @@ -111,7 +178,7 @@ public void testInstallFileWithClassifier() assertNotNull( mojo ); - setVariableValueToObject( mojo, "session", createMavenSession() ); + setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) ); assignValuesForParameter( mojo ); @@ -137,7 +204,7 @@ public void testInstallFileWithGeneratePom() assertNotNull( mojo ); - setVariableValueToObject( mojo, "session", createMavenSession() ); + setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) ); assignValuesForParameter( mojo ); @@ -178,7 +245,7 @@ public void testInstallFileWithPomFile() assertNotNull( mojo ); - setVariableValueToObject( mojo, "session", createMavenSession() ); + setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) ); assignValuesForParameter( mojo ); @@ -211,7 +278,7 @@ public void testInstallFileWithPomAsPackaging() assertNotNull( mojo ); - setVariableValueToObject( mojo, "session", createMavenSession() ); + setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) ); assignValuesForParameter( mojo ); @@ -239,7 +306,7 @@ public void testInstallFile() assertNotNull( mojo ); - setVariableValueToObject( mojo, "session", createMavenSession() ); + setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) ); assignValuesForParameter( mojo ); @@ -276,13 +343,14 @@ private String dotToSlashReplacer( String parameter ) return parameter.replace( '.', '/' ); } - private MavenSession createMavenSession() throws NoLocalRepositoryManagerException + private MavenSession createMavenSession( String localRepositoryBaseDir ) + throws NoLocalRepositoryManagerException { MavenSession session = mock( MavenSession.class ); DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession(); repositorySession.setLocalRepositoryManager( new EnhancedLocalRepositoryManagerFactory().newInstance( - repositorySession, new LocalRepository( LOCAL_REPO ) + repositorySession, new LocalRepository( localRepositoryBaseDir ) ) ); ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); diff --git a/src/test/resources/unit/install-file-from-local-repository-test/plugin-config.xml b/src/test/resources/unit/install-file-from-local-repository-test/plugin-config.xml new file mode 100644 index 00000000..31ddebf6 --- /dev/null +++ b/src/test/resources/unit/install-file-from-local-repository-test/plugin-config.xml @@ -0,0 +1,35 @@ + + + + + + maven-install-plugin + + org.apache.maven.test + maven-install-test + 1.0-SNAPSHOT + jar + ${basedir}/target/test-classes/unit/install-file-from-local-repository-test/target/org/apache/maven/test/maven-install-test/1.0-SNAPSHOT/maven-install-test-1.0-SNAPSHOT.jar + + + + + + diff --git a/src/test/resources/unit/install-file-from-local-repository-test/target/org/apache/maven/test/maven-install-test/1.0-SNAPSHOT/maven-install-test-1.0-SNAPSHOT.jar b/src/test/resources/unit/install-file-from-local-repository-test/target/org/apache/maven/test/maven-install-test/1.0-SNAPSHOT/maven-install-test-1.0-SNAPSHOT.jar new file mode 100644 index 00000000..4a0e834e Binary files /dev/null and b/src/test/resources/unit/install-file-from-local-repository-test/target/org/apache/maven/test/maven-install-test/1.0-SNAPSHOT/maven-install-test-1.0-SNAPSHOT.jar differ