Skip to content

Commit 4cbbc08

Browse files
rbioteauslawekjaranowski
authored andcommitted
[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.
1 parent bb53609 commit 4cbbc08

File tree

5 files changed

+115
-11
lines changed

5 files changed

+115
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
target/
2+
!src/test/resources/unit/*/target
23
.project
34
.classpath
45
.settings/

src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ public void execute()
248248
).setFile( file );
249249
installRequest.addArtifact( mainArtifact );
250250

251-
File artifactLocalFile = getLocalRepositoryFile( session.getRepositorySession(), mainArtifact );
252-
File pomLocalFile = getPomLocalRepositoryFile( session.getRepositorySession(), mainArtifact );
251+
File artifactLocalFile = getLocalRepositoryFile( repositorySystemSession, mainArtifact );
252+
File pomLocalFile = getPomLocalRepositoryFile( repositorySystemSession, mainArtifact );
253253

254254
if ( file.equals( artifactLocalFile ) )
255255
{

src/test/java/org/apache/maven/plugins/install/InstallFileMojoTest.java

Lines changed: 77 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,79 @@ public class InstallFileMojoTest
5858

5959
private final String LOCAL_REPO = "target/local-repo/";
6060

61+
private final String SPECIFIC_LOCAL_REPO = "target/specific-local-repo/";
62+
6163
public void setUp()
6264
throws Exception
6365
{
6466
super.setUp();
6567

6668
FileUtils.deleteDirectory( new File( getBasedir() + "/" + LOCAL_REPO ) );
69+
FileUtils.deleteDirectory( new File( getBasedir() + "/" + SPECIFIC_LOCAL_REPO ) );
70+
}
71+
72+
public void testInstallFileFromLocalRepositoryToLocalRepositoryPath()
73+
throws Exception
74+
{
75+
File localRepository =
76+
new File( getBasedir(), "target/test-classes/unit/install-file-from-local-repository-test/target" );
77+
78+
File testPom = new File( localRepository.getParentFile(), "plugin-config.xml" );
79+
80+
InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );
81+
82+
assertNotNull( mojo );
83+
84+
setVariableValueToObject( mojo, "session", createMavenSession( localRepository.getAbsolutePath() ) );
85+
86+
File specificLocalRepositoryPath = new File( getBasedir() + "/" + SPECIFIC_LOCAL_REPO );
87+
88+
setVariableValueToObject( mojo, "localRepositoryPath", specificLocalRepositoryPath );
89+
90+
assignValuesForParameter( mojo );
91+
92+
mojo.execute();
93+
94+
String localPath = getBasedir() + "/" + SPECIFIC_LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/"
95+
+ artifactId + "-" + version;
96+
97+
File installedArtifact = new File( localPath + "." + "jar" );
98+
99+
assertTrue( installedArtifact.exists() );
100+
101+
assertEquals( FileUtils.getFiles( new File( SPECIFIC_LOCAL_REPO ), null, null ).toString(), 5,
102+
FileUtils.getFiles( new File( SPECIFIC_LOCAL_REPO ), null, null ).size() );
103+
}
104+
105+
public void testInstallFileWithLocalRepositoryPath()
106+
throws Exception
107+
{
108+
File testPom =
109+
new File( getBasedir(), "target/test-classes/unit/install-file-with-checksum/" + "plugin-config.xml" );
110+
111+
InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );
112+
113+
assertNotNull( mojo );
114+
115+
setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) );
116+
117+
File specificLocalRepositoryPath = new File( getBasedir() + "/" + SPECIFIC_LOCAL_REPO );
118+
119+
setVariableValueToObject( mojo, "localRepositoryPath", specificLocalRepositoryPath );
120+
121+
assignValuesForParameter( mojo );
122+
123+
mojo.execute();
124+
125+
String localPath = getBasedir() + "/" + SPECIFIC_LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/"
126+
+ artifactId + "-" + version;
127+
128+
File installedArtifact = new File( localPath + "." + "jar" );
129+
130+
assertTrue( installedArtifact.exists() );
131+
132+
assertEquals( FileUtils.getFiles( new File( SPECIFIC_LOCAL_REPO ), null, null ).toString(), 5,
133+
FileUtils.getFiles( new File( SPECIFIC_LOCAL_REPO ), null, null ).size() );
67134
}
68135

69136
public void testInstallFileTestEnvironment()
@@ -73,7 +140,7 @@ public void testInstallFileTestEnvironment()
73140

74141
InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );
75142

76-
setVariableValueToObject( mojo, "session", createMavenSession() );
143+
setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) );
77144

78145
assertNotNull( mojo );
79146
}
@@ -87,7 +154,7 @@ public void testBasicInstallFile()
87154

88155
assertNotNull( mojo );
89156

90-
setVariableValueToObject( mojo, "session", createMavenSession() );
157+
setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) );
91158

92159
assignValuesForParameter( mojo );
93160

@@ -111,7 +178,7 @@ public void testInstallFileWithClassifier()
111178

112179
assertNotNull( mojo );
113180

114-
setVariableValueToObject( mojo, "session", createMavenSession() );
181+
setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) );
115182

116183
assignValuesForParameter( mojo );
117184

@@ -137,7 +204,7 @@ public void testInstallFileWithGeneratePom()
137204

138205
assertNotNull( mojo );
139206

140-
setVariableValueToObject( mojo, "session", createMavenSession() );
207+
setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) );
141208

142209
assignValuesForParameter( mojo );
143210

@@ -178,7 +245,7 @@ public void testInstallFileWithPomFile()
178245

179246
assertNotNull( mojo );
180247

181-
setVariableValueToObject( mojo, "session", createMavenSession() );
248+
setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) );
182249

183250
assignValuesForParameter( mojo );
184251

@@ -211,7 +278,7 @@ public void testInstallFileWithPomAsPackaging()
211278

212279
assertNotNull( mojo );
213280

214-
setVariableValueToObject( mojo, "session", createMavenSession() );
281+
setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) );
215282

216283
assignValuesForParameter( mojo );
217284

@@ -239,7 +306,7 @@ public void testInstallFile()
239306

240307
assertNotNull( mojo );
241308

242-
setVariableValueToObject( mojo, "session", createMavenSession() );
309+
setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) );
243310

244311
assignValuesForParameter( mojo );
245312

@@ -276,13 +343,14 @@ private String dotToSlashReplacer( String parameter )
276343
return parameter.replace( '.', '/' );
277344
}
278345

279-
private MavenSession createMavenSession() throws NoLocalRepositoryManagerException
346+
private MavenSession createMavenSession( String localRepositoryBaseDir )
347+
throws NoLocalRepositoryManagerException
280348
{
281349
MavenSession session = mock( MavenSession.class );
282350
DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
283351
repositorySession.setLocalRepositoryManager(
284352
new EnhancedLocalRepositoryManagerFactory().newInstance(
285-
repositorySession, new LocalRepository( LOCAL_REPO )
353+
repositorySession, new LocalRepository( localRepositoryBaseDir )
286354
)
287355
);
288356
ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<!--
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
-->
19+
<project>
20+
<build>
21+
<plugins>
22+
<plugin>
23+
<artifactId>maven-install-plugin</artifactId>
24+
<configuration>
25+
<groupId>org.apache.maven.test</groupId>
26+
<artifactId>maven-install-test</artifactId>
27+
<version>1.0-SNAPSHOT</version>
28+
<packaging>jar</packaging>
29+
<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
30+
</file>
31+
</configuration>
32+
</plugin>
33+
</plugins>
34+
</build>
35+
</project>

0 commit comments

Comments
 (0)