Skip to content

Commit 79289f4

Browse files
[MINVOKER-328] Skip install artifacts with the same source and target path
When user not specified localRepositoryPath we can not reinstall artifact with the same source and target path
1 parent 6bb7c6b commit 79289f4

File tree

4 files changed

+127
-2
lines changed

4 files changed

+127
-2
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
invoker.goals = verify
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!--
4+
Licensed to the Apache Software Foundation (ASF) under one
5+
or more contributor license agreements. See the NOTICE file
6+
distributed with this work for additional information
7+
regarding copyright ownership. The ASF licenses this file
8+
to you under the Apache License, Version 2.0 (the
9+
"License"); you may not use this file except in compliance
10+
with the License. You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0
13+
14+
Unless required by applicable law or agreed to in writing,
15+
software distributed under the License is distributed on an
16+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
KIND, either express or implied. See the License for the
18+
specific language governing permissions and limitations
19+
under the License.
20+
-->
21+
22+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
24+
<modelVersion>4.0.0</modelVersion>
25+
26+
<groupId>org.apache.maven.plugins.invoker</groupId>
27+
<artifactId>minvoker328</artifactId>
28+
<version>1.0-SNAPSHOT</version>
29+
<packaging>pom</packaging>
30+
31+
<url>https://issues.apache.org/jira/browse/MINVOKER-328</url>
32+
<description>install with default configuration</description>
33+
34+
<properties>
35+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
36+
</properties>
37+
38+
<dependencies>
39+
<dependency>
40+
<groupId>org.slf4j</groupId>
41+
<artifactId>slf4j-api</artifactId>
42+
<version>1.7.36</version>
43+
</dependency>
44+
</dependencies>
45+
46+
<build>
47+
<plugins>
48+
<plugin>
49+
<groupId>org.apache.maven.plugins</groupId>
50+
<artifactId>maven-invoker-plugin</artifactId>
51+
<version>@project.version@</version>
52+
<executions>
53+
<execution>
54+
<goals>
55+
<goal>install</goal>
56+
</goals>
57+
</execution>
58+
</executions>
59+
</plugin>
60+
</plugins>
61+
</build>
62+
63+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
File buildLog = new File(basedir, 'build.log')
20+
assert buildLog.text.contains('[DEBUG] Skip install the same target')

src/main/java/org/apache/maven/plugins/invoker/InstallMojo.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@
1919
package org.apache.maven.plugins.invoker;
2020

2121
import java.io.File;
22+
import java.io.IOException;
23+
import java.io.UncheckedIOException;
2224
import java.util.ArrayList;
2325
import java.util.Collection;
2426
import java.util.Collections;
2527
import java.util.LinkedHashMap;
2628
import java.util.List;
2729
import java.util.Map;
30+
import java.util.Objects;
2831
import java.util.Optional;
2932
import java.util.stream.Collectors;
3033

@@ -356,6 +359,10 @@ private Artifact resolveArtifact(Artifact artifact, List<RemoteRepository> remot
356359
*/
357360
private void installArtifacts(Collection<Artifact> resolvedArtifacts) throws InstallationException {
358361

362+
RepositorySystemSession systemSessionForLocalRepo = createSystemSessionForLocalRepo();
363+
// remove artifacts with tha same target and source file
364+
resolvedArtifacts.removeIf(a -> hasTheSamePathAsTarget(a, systemSessionForLocalRepo));
365+
359366
// we can have on dependency two artifacts with the same groupId:artifactId
360367
// with different version, in such case when we install both in one request
361368
// metadata will contain only one version
@@ -366,15 +373,32 @@ private void installArtifacts(Collection<Artifact> resolvedArtifacts) throws Ins
366373
LinkedHashMap::new,
367374
Collectors.toList()));
368375

369-
RepositorySystemSession systemSessionForLocalRepo = createSystemSessionForLocalRepo();
370-
371376
for (List<Artifact> artifacts : collect.values()) {
372377
InstallRequest request = new InstallRequest();
373378
request.setArtifacts(artifacts);
374379
repositorySystem.install(systemSessionForLocalRepo, request);
375380
}
376381
}
377382

383+
private boolean hasTheSamePathAsTarget(Artifact artifact, RepositorySystemSession systemSession) {
384+
try {
385+
LocalRepositoryManager lrm = systemSession.getLocalRepositoryManager();
386+
File targetBasedir = lrm.getRepository().getBasedir();
387+
if (targetBasedir == null) {
388+
return false;
389+
}
390+
File targetFile = new File(targetBasedir, lrm.getPathForLocalArtifact(artifact)).getCanonicalFile();
391+
File sourceFile = artifact.getFile().getCanonicalFile();
392+
if (Objects.equals(targetFile, sourceFile)) {
393+
getLog().debug("Skip install the same target " + sourceFile);
394+
return true;
395+
}
396+
return false;
397+
} catch (IOException e) {
398+
throw new UncheckedIOException(e);
399+
}
400+
}
401+
378402
/**
379403
* Create a new {@link RepositorySystemSession} connected with local repo.
380404
*/

0 commit comments

Comments
 (0)