From de1dbbf47da0d6bc48d2986651aa547ca8fa5a8f Mon Sep 17 00:00:00 2001 From: Slawomir Jaranowski Date: Sat, 25 Feb 2023 12:22:13 +0100 Subject: [PATCH] [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 --- .../invoker.properties | 18 ++++++ src/it/MINVOKER-328_install-default/pom.xml | 63 +++++++++++++++++++ .../verify.groovy | 20 ++++++ .../maven/plugins/invoker/InstallMojo.java | 27 +++++++- 4 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 src/it/MINVOKER-328_install-default/invoker.properties create mode 100644 src/it/MINVOKER-328_install-default/pom.xml create mode 100644 src/it/MINVOKER-328_install-default/verify.groovy diff --git a/src/it/MINVOKER-328_install-default/invoker.properties b/src/it/MINVOKER-328_install-default/invoker.properties new file mode 100644 index 00000000..353946b6 --- /dev/null +++ b/src/it/MINVOKER-328_install-default/invoker.properties @@ -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 diff --git a/src/it/MINVOKER-328_install-default/pom.xml b/src/it/MINVOKER-328_install-default/pom.xml new file mode 100644 index 00000000..8b2718b3 --- /dev/null +++ b/src/it/MINVOKER-328_install-default/pom.xml @@ -0,0 +1,63 @@ + + + + + + 4.0.0 + + org.apache.maven.plugins.invoker + minvoker328 + 1.0-SNAPSHOT + pom + + https://issues.apache.org/jira/browse/MINVOKER-328 + install with default configuration + + + UTF-8 + + + + + org.slf4j + slf4j-api + 1.7.36 + + + + + + + org.apache.maven.plugins + maven-invoker-plugin + @project.version@ + + + + install + + + + + + + + diff --git a/src/it/MINVOKER-328_install-default/verify.groovy b/src/it/MINVOKER-328_install-default/verify.groovy new file mode 100644 index 00000000..144091e7 --- /dev/null +++ b/src/it/MINVOKER-328_install-default/verify.groovy @@ -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') \ No newline at end of file diff --git a/src/main/java/org/apache/maven/plugins/invoker/InstallMojo.java b/src/main/java/org/apache/maven/plugins/invoker/InstallMojo.java index 8c2000d4..e9fc917a 100644 --- a/src/main/java/org/apache/maven/plugins/invoker/InstallMojo.java +++ b/src/main/java/org/apache/maven/plugins/invoker/InstallMojo.java @@ -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; @@ -356,18 +359,19 @@ private Artifact resolveArtifact(Artifact artifact, List remot */ private void installArtifacts(Collection 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> 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 artifacts : collect.values()) { InstallRequest request = new InstallRequest(); request.setArtifacts(artifacts); @@ -375,6 +379,25 @@ private void installArtifacts(Collection resolvedArtifacts) throws Ins } } + 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. */