Skip to content

Commit

Permalink
[MINVOKER-328] Skip install artifacts with the same source and target…
Browse files Browse the repository at this point in the history
… path

When user not specified localRepositoryPath
we can not reinstall artifact with the same source and target path
  • Loading branch information
slawekjaranowski committed Mar 3, 2023
1 parent c7fcdbf commit de1dbbf
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/it/MINVOKER-328_install-default/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 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.

invoker.goals = verify
63 changes: 63 additions & 0 deletions src/it/MINVOKER-328_install-default/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
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 xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.plugins.invoker</groupId>
<artifactId>minvoker328</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<url>https://issues.apache.org/jira/browse/MINVOKER-328</url>
<description>install with default configuration</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-invoker-plugin</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<goals>
<goal>install</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
20 changes: 20 additions & 0 deletions src/it/MINVOKER-328_install-default/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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.
*/
File buildLog = new File(basedir, 'build.log')
assert buildLog.text.contains('[DEBUG] Skip install the same target')
27 changes: 25 additions & 2 deletions src/main/java/org/apache/maven/plugins/invoker/InstallMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@
package org.apache.maven.plugins.invoker;

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -356,25 +359,45 @@ private Artifact resolveArtifact(Artifact artifact, List<RemoteRepository> remot
*/
private void installArtifacts(Collection<Artifact> resolvedArtifacts) throws InstallationException {

RepositorySystemSession systemSessionForLocalRepo = createSystemSessionForLocalRepo();

// we can have on dependency two artifacts with the same groupId:artifactId
// with different version, in such case when we install both in one request
// metadata will contain only one version

Map<String, List<Artifact>> collect = resolvedArtifacts.stream()
.filter(a -> !hasTheSamePathAsTarget(a, systemSessionForLocalRepo))
.collect(Collectors.groupingBy(
a -> String.format("%s:%s:%s", a.getGroupId(), a.getArtifactId(), a.getVersion()),
LinkedHashMap::new,
Collectors.toList()));

RepositorySystemSession systemSessionForLocalRepo = createSystemSessionForLocalRepo();

for (List<Artifact> artifacts : collect.values()) {
InstallRequest request = new InstallRequest();
request.setArtifacts(artifacts);
repositorySystem.install(systemSessionForLocalRepo, request);
}
}

private boolean hasTheSamePathAsTarget(Artifact artifact, RepositorySystemSession systemSession) {
try {
LocalRepositoryManager lrm = systemSession.getLocalRepositoryManager();
File targetBasedir = lrm.getRepository().getBasedir();
if (targetBasedir == null) {
return false;
}
File targetFile = new File(targetBasedir, lrm.getPathForLocalArtifact(artifact)).getCanonicalFile();
File sourceFile = artifact.getFile().getCanonicalFile();
if (Objects.equals(targetFile, sourceFile)) {
getLog().debug("Skip install the same target " + sourceFile);
return true;
}
return false;
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

/**
* Create a new {@link RepositorySystemSession} connected with local repo.
*/
Expand Down

0 comments on commit de1dbbf

Please sign in to comment.