You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sj...@apache.org on 2022/12/13 23:56:55 UTC

[maven-install-plugin] branch master updated: [MINSTALL-186] Use proper repositorySystemSession

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 4cbbc08  [MINSTALL-186] Use proper repositorySystemSession
4cbbc08 is described below

commit 4cbbc08d5114539f81519db392ad1b4e401e394c
Author: Romain Bioteau <ro...@bonitasoft.com>
AuthorDate: Fri Nov 25 14:55:54 2022 +0100

    [MINSTALL-186] Use proper repositorySystemSession
    
    When looking for existing files in the local repository, use the proper
    `repositorySystemSession`.
    
    Current behavior ignore the `localRepositoryPath` parameter when
    checking for file existence and lead to false positive.
---
 .gitignore                                         |   1 +
 .../maven/plugins/install/InstallFileMojo.java     |   4 +-
 .../maven/plugins/install/InstallFileMojoTest.java |  86 ++++++++++++++++++---
 .../plugin-config.xml                              |  35 +++++++++
 .../maven-install-test-1.0-SNAPSHOT.jar            | Bin 0 -> 2372 bytes
 5 files changed, 115 insertions(+), 11 deletions(-)

diff --git a/.gitignore b/.gitignore
index 7495d7e..3093e95 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 a006dac..bdd46c1 100644
--- a/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java
+++ b/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java
@@ -248,8 +248,8 @@ public class InstallFileMojo
         ).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 7efd90c..bcfd69d 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 class InstallFileMojoTest
 
         InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );
         
-        setVariableValueToObject( mojo, "session", createMavenSession() );
+        setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) );
 
         assertNotNull( mojo );
     }
@@ -87,7 +154,7 @@ public class InstallFileMojoTest
 
         assertNotNull( mojo );
         
-        setVariableValueToObject( mojo, "session", createMavenSession() );
+        setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) );
 
         assignValuesForParameter( mojo );
 
@@ -111,7 +178,7 @@ public class InstallFileMojoTest
 
         assertNotNull( mojo );
         
-        setVariableValueToObject( mojo, "session", createMavenSession() );
+        setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) );
 
         assignValuesForParameter( mojo );
 
@@ -137,7 +204,7 @@ public class InstallFileMojoTest
 
         assertNotNull( mojo );
         
-        setVariableValueToObject( mojo, "session", createMavenSession() );
+        setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) );
 
         assignValuesForParameter( mojo );
 
@@ -178,7 +245,7 @@ public class InstallFileMojoTest
 
         assertNotNull( mojo );
         
-        setVariableValueToObject( mojo, "session", createMavenSession() );
+        setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) );
 
         assignValuesForParameter( mojo );
 
@@ -211,7 +278,7 @@ public class InstallFileMojoTest
 
         assertNotNull( mojo );
         
-        setVariableValueToObject( mojo, "session", createMavenSession() );
+        setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) );
 
         assignValuesForParameter( mojo );
 
@@ -239,7 +306,7 @@ public class InstallFileMojoTest
 
         assertNotNull( mojo );
         
-        setVariableValueToObject( mojo, "session", createMavenSession() );
+        setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) );
 
         assignValuesForParameter( mojo );
 
@@ -276,13 +343,14 @@ public class InstallFileMojoTest
         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 0000000..31ddebf
--- /dev/null
+++ b/src/test/resources/unit/install-file-from-local-repository-test/plugin-config.xml
@@ -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.
+-->
+<project>
+  <build>
+    <plugins>
+      <plugin>
+        <artifactId>maven-install-plugin</artifactId>
+        <configuration>
+          <groupId>org.apache.maven.test</groupId>
+          <artifactId>maven-install-test</artifactId>
+          <version>1.0-SNAPSHOT</version>
+          <packaging>jar</packaging>
+          <file>${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
+          </file>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>
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 0000000..4a0e834
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