From 3ebb44860673e141e3d5f680df434cd5ac61d46b Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Fri, 12 Jan 2024 21:02:37 +0100 Subject: [PATCH] [MINSTALL-197] Update to parent 41, cleanup (#61) In general perform a cleanup of plugin, apply updates where needed. The plugin now builds against 3.9.x latest, and minimum is left as is (3.2.5). A notable change along that above is removal of "baked in" packaging, now plugin relies completely and only on what project and artifact tells, there is no "if this is POM" check anymore. The new "BOM UT" shows this in action. Has to be noted though, that the UT tests ONLY the plugin, to use BOM packaging you need Maven that knows what it is (Maven 3 w/ extension or Maven 4 that has it OOTB). --- https://issues.apache.org/jira/browse/MINSTALL-197 --- pom.xml | 61 +++++++++----- .../plugins/install/InstallFileMojo.java | 8 +- .../maven/plugins/install/InstallMojo.java | 40 +++++++--- .../plugins/install/InstallFileMojoTest.java | 8 +- .../plugins/install/InstallMojoTest.java | 80 +++++++++++++++---- .../install/stubs/InstallArtifactStub.java | 6 +- .../install/stubs/InstallBomArtifactStub.java | 37 +++++++++ .../install/stubs/InstallPomArtifactStub.java | 37 +++++++++ .../plugin-config.xml | 35 ++++++++ .../plugin-config.xml | 2 +- 10 files changed, 258 insertions(+), 56 deletions(-) create mode 100644 src/test/java/org/apache/maven/plugins/install/stubs/InstallBomArtifactStub.java create mode 100644 src/test/java/org/apache/maven/plugins/install/stubs/InstallPomArtifactStub.java create mode 100644 src/test/resources/unit/basic-install-test-packaging-bom/plugin-config.xml diff --git a/pom.xml b/pom.xml index 1f4d7c14..7925f001 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,7 @@ org.apache.maven.plugins maven-plugins - 39 + 41 @@ -46,7 +46,7 @@ - ${mavenVersion} + 3.2.5 @@ -72,21 +72,21 @@ 8 - 3.2.5 + 3.9.6 - 1.0.0.v20140518 + 1.9.18 - 1.7.5 + 1.7.36 - 3.1.0 - 3.10.1 - 3.1.0 - 3.3.0 - 3.7.0 - 3.3.0 - 3.2.1 - 3.0.0-M7 + ${version.maven-antrun-plugin} + ${version.maven-compiler-plugin} + ${version.maven-enforcer-plugin} + ${version.maven-jar-plugin} + ${version.maven-plugin-tools} + ${version.maven-resources-plugin} + ${version.maven-source-plugin} + ${version.maven-surefire} 2023-03-21T14:31:18Z @@ -118,15 +118,15 @@ provided - org.eclipse.aether - aether-api - ${aetherVersion} + org.apache.maven.resolver + maven-resolver-api + ${resolverVersion} provided - org.eclipse.aether - aether-util - ${aetherVersion} + org.apache.maven.resolver + maven-resolver-util + ${resolverVersion} compile @@ -134,6 +134,10 @@ org.codehaus.plexus plexus-utils + + org.codehaus.plexus + plexus-xml + @@ -180,13 +184,26 @@ test - org.eclipse.aether - aether-impl - ${aetherVersion} + org.apache.maven.resolver + maven-resolver-impl + ${resolverVersion} test + + + + org.apache.maven.plugins + maven-compiler-plugin + + none + true + + + + + run-its 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 021b5c4b..4ba6b641 100644 --- a/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java +++ b/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java @@ -67,7 +67,7 @@ */ @Mojo(name = "install-file", requiresProject = false, aggregator = true, threadSafe = true) public class InstallFileMojo extends AbstractMojo { - private static final String LS = System.getProperty("line.separator"); + private static final String LS = System.lineSeparator(); @Component private RepositorySystem repositorySystem; @@ -384,11 +384,11 @@ private Model generateModel() { private File generatePomFile() throws MojoExecutionException { Model model = generateModel(); try { - File tempPomFile = File.createTempFile("mvninstall", ".pom"); + Path tempPomFile = Files.createTempFile("mvninstall", ".pom"); - try (OutputStream writer = Files.newOutputStream(tempPomFile.toPath())) { + try (OutputStream writer = Files.newOutputStream(tempPomFile)) { new MavenXpp3Writer().write(writer, model); - return tempPomFile; + return tempPomFile.toFile(); } } catch (IOException e) { throw new MojoExecutionException("Error writing temporary POM file: " + e.getMessage(), e); diff --git a/src/main/java/org/apache/maven/plugins/install/InstallMojo.java b/src/main/java/org/apache/maven/plugins/install/InstallMojo.java index c14b6a3c..a2aaa9a0 100644 --- a/src/main/java/org/apache/maven/plugins/install/InstallMojo.java +++ b/src/main/java/org/apache/maven/plugins/install/InstallMojo.java @@ -38,8 +38,10 @@ import org.apache.maven.project.MavenProject; import org.apache.maven.project.artifact.ProjectArtifact; import org.eclipse.aether.RepositorySystem; +import org.eclipse.aether.artifact.Artifact; import org.eclipse.aether.installation.InstallRequest; import org.eclipse.aether.installation.InstallationException; +import org.eclipse.aether.util.artifact.ArtifactIdUtils; /** * Installs the project's main artifact, and any other artifacts attached by other plugins in the lifecycle, to the @@ -58,9 +60,6 @@ public class InstallMojo extends AbstractMojo { @Parameter(defaultValue = "${project}", readonly = true, required = true) private MavenProject project; - @Parameter(defaultValue = "${reactorProjects}", required = true, readonly = true) - private List reactorProjects; - @Parameter(defaultValue = "${plugin}", required = true, readonly = true) private PluginDescriptor pluginDescriptor; @@ -157,7 +156,9 @@ private boolean allProjectsMarked(List allProjectsUsingPlugin) { rp -> hasExecution(rp.getPlugin("org.apache.maven.plugins:maven-install-plugin")); private List getAllProjectsUsingPlugin() { - return reactorProjects.stream().filter(hasMavenInstallPluginExecution).collect(Collectors.toList()); + return session.getProjects().stream() + .filter(hasMavenInstallPluginExecution) + .collect(Collectors.toList()); } private final Predicate havingGoals = pe -> !pe.getGoals().isEmpty(); @@ -185,16 +186,35 @@ private void installProject(InstallRequest request) throws MojoExecutionExceptio * @throws MojoExecutionException if project is badly set up. */ private void processProject(MavenProject project, InstallRequest request) throws MojoExecutionException { - if (isFile(project.getFile())) { - request.addArtifact(RepositoryUtils.toArtifact(new ProjectArtifact(project))); + // always exists, as project exists + Artifact pomArtifact = RepositoryUtils.toArtifact(new ProjectArtifact(project)); + // always exists, but at "init" is w/o file (packaging plugin assigns file to this when packaged) + Artifact projectArtifact = RepositoryUtils.toArtifact(project.getArtifact()); + + // pom project: pomArtifact and projectArtifact are SAME + // jar project: pomArtifact and projectArtifact are DIFFERENT + // incomplete project: is not pom project and projectArtifact has no file + + // we must compare coordinates ONLY (as projectArtifact may not have file, and Artifact.equals factors it in) + // BUT if projectArtifact has file set, use that one + if (ArtifactIdUtils.equalsId(pomArtifact, projectArtifact)) { + if (isFile(projectArtifact.getFile())) { + pomArtifact = projectArtifact; + } + projectArtifact = null; + } + + if (isFile(pomArtifact.getFile())) { + request.addArtifact(pomArtifact); } else { throw new MojoExecutionException("The project POM could not be attached"); } - if (!"pom".equals(project.getPackaging())) { - org.apache.maven.artifact.Artifact mavenMainArtifact = project.getArtifact(); - if (isFile(mavenMainArtifact.getFile())) { - request.addArtifact(RepositoryUtils.toArtifact(mavenMainArtifact)); + // is not packaged, is "incomplete" + boolean isIncomplete = projectArtifact != null && !isFile(projectArtifact.getFile()); + if (projectArtifact != null) { + if (!isIncomplete) { + request.addArtifact(projectArtifact); } else if (!project.getAttachedArtifacts().isEmpty()) { if (allowIncompleteProjects) { getLog().warn(""); 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 8174e8a1..5094468a 100644 --- a/src/test/java/org/apache/maven/plugins/install/InstallFileMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/install/InstallFileMojoTest.java @@ -30,6 +30,9 @@ import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.xml.XmlStreamReader; import org.eclipse.aether.DefaultRepositorySystemSession; +import org.eclipse.aether.internal.impl.DefaultLocalPathComposer; +import org.eclipse.aether.internal.impl.DefaultLocalPathPrefixComposerFactory; +import org.eclipse.aether.internal.impl.DefaultTrackingFileManager; import org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManagerFactory; import org.eclipse.aether.repository.LocalRepository; import org.eclipse.aether.repository.NoLocalRepositoryManagerException; @@ -341,7 +344,10 @@ private String dotToSlashReplacer(String parameter) { private MavenSession createMavenSession(String localRepositoryBaseDir) throws NoLocalRepositoryManagerException { MavenSession session = mock(MavenSession.class); DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession(); - repositorySession.setLocalRepositoryManager(new EnhancedLocalRepositoryManagerFactory() + repositorySession.setLocalRepositoryManager(new EnhancedLocalRepositoryManagerFactory( + new DefaultLocalPathComposer(), + new DefaultTrackingFileManager(), + new DefaultLocalPathPrefixComposerFactory()) .newInstance(repositorySession, new LocalRepository(localRepositoryBaseDir))); ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); buildingRequest.setRepositorySession(repositorySession); diff --git a/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java b/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java index 1de40fef..79397787 100644 --- a/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java @@ -39,6 +39,9 @@ import org.codehaus.plexus.util.FileUtils; import org.eclipse.aether.DefaultRepositorySystemSession; import org.eclipse.aether.artifact.DefaultArtifact; +import org.eclipse.aether.internal.impl.DefaultLocalPathComposer; +import org.eclipse.aether.internal.impl.DefaultLocalPathPrefixComposerFactory; +import org.eclipse.aether.internal.impl.DefaultTrackingFileManager; import org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManagerFactory; import org.eclipse.aether.repository.LocalRepository; import org.eclipse.aether.repository.NoLocalRepositoryManagerException; @@ -84,10 +87,11 @@ public void testBasicInstall() throws Exception { MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project"); updateMavenProject(project); + MavenSession session = createMavenSession(); + session.setProjects(Collections.singletonList(project)); + setVariableValueToObject(mojo, "session", session); setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>()); setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor()); - setVariableValueToObject(mojo, "reactorProjects", Collections.singletonList(project)); - setVariableValueToObject(mojo, "session", createMavenSession()); artifact = (InstallArtifactStub) project.getArtifact(); @@ -120,10 +124,11 @@ public void testBasicInstallWithAttachedArtifacts() throws Exception { MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project"); updateMavenProject(project); + MavenSession session = createMavenSession(); + session.setProjects(Collections.singletonList(project)); + setVariableValueToObject(mojo, "session", session); setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>()); setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor()); - setVariableValueToObject(mojo, "reactorProjects", Collections.singletonList(project)); - setVariableValueToObject(mojo, "session", createMavenSession()); List attachedArtifacts = project.getAttachedArtifacts(); @@ -164,10 +169,11 @@ public void testUpdateReleaseParamSetToTrue() throws Exception { MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project"); updateMavenProject(project); + MavenSession session = createMavenSession(); + session.setProjects(Collections.singletonList(project)); + setVariableValueToObject(mojo, "session", session); setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>()); setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor()); - setVariableValueToObject(mojo, "reactorProjects", Collections.singletonList(project)); - setVariableValueToObject(mojo, "session", createMavenSession()); artifact = (InstallArtifactStub) project.getArtifact(); @@ -190,10 +196,11 @@ public void testInstallIfArtifactFileIsNull() throws Exception { MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project"); updateMavenProject(project); + MavenSession session = createMavenSession(); + session.setProjects(Collections.singletonList(project)); + setVariableValueToObject(mojo, "session", session); setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>()); setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor()); - setVariableValueToObject(mojo, "reactorProjects", Collections.singletonList(project)); - setVariableValueToObject(mojo, "session", createMavenSession()); artifact = (InstallArtifactStub) project.getArtifact(); @@ -223,10 +230,11 @@ public void testInstallIfPackagingIsPom() throws Exception { MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project"); updateMavenProject(project); + MavenSession session = createMavenSession(); + session.setProjects(Collections.singletonList(project)); + setVariableValueToObject(mojo, "session", session); setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>()); setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor()); - setVariableValueToObject(mojo, "reactorProjects", Collections.singletonList(project)); - setVariableValueToObject(mojo, "session", createMavenSession()); String packaging = project.getPackaging(); @@ -248,6 +256,43 @@ public void testInstallIfPackagingIsPom() throws Exception { assertEquals(4, FileUtils.getFiles(new File(LOCAL_REPO), null, null).size()); } + public void testInstallIfPackagingIsBom() throws Exception { + File testPom = new File( + getBasedir(), "target/test-classes/unit/basic-install-test-packaging-bom/" + "plugin-config.xml"); + + AbstractMojo mojo = (AbstractMojo) lookupMojo("install", testPom); + + assertNotNull(mojo); + + MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project"); + updateMavenProject(project); + + MavenSession session = createMavenSession(); + session.setProjects(Collections.singletonList(project)); + setVariableValueToObject(mojo, "session", session); + setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>()); + setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor()); + + String packaging = project.getPackaging(); + + assertEquals("bom", packaging); + + artifact = (InstallArtifactStub) project.getArtifact(); + + mojo.execute(); + + String groupId = dotToSlashReplacer(artifact.getGroupId()); + + File installedArtifact = new File( + getBasedir(), + LOCAL_REPO + groupId + "/" + artifact.getArtifactId() + "/" + artifact.getVersion() + "/" + + artifact.getArtifactId() + "-" + artifact.getVersion() + "." + "pom"); + + assertTrue(installedArtifact.exists()); + + assertEquals(4, FileUtils.getFiles(new File(LOCAL_REPO), null, null).size()); + } + public void testBasicInstallAndCreate() throws Exception { File testPom = new File(getBasedir(), "target/test-classes/unit/basic-install-checksum/plugin-config.xml"); @@ -261,10 +306,11 @@ public void testBasicInstallAndCreate() throws Exception { MavenSession mavenSession = createMavenSession(); updateMavenProject(project); + MavenSession session = createMavenSession(); + session.setProjects(Collections.singletonList(project)); + setVariableValueToObject(mojo, "session", session); setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>()); setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor()); - setVariableValueToObject(mojo, "reactorProjects", Collections.singletonList(project)); - setVariableValueToObject(mojo, "session", mavenSession); artifact = (InstallArtifactStub) project.getArtifact(); @@ -308,10 +354,11 @@ public void testSkip() throws Exception { MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project"); updateMavenProject(project); + MavenSession session = createMavenSession(); + session.setProjects(Collections.singletonList(project)); + setVariableValueToObject(mojo, "session", session); setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>()); setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor()); - setVariableValueToObject(mojo, "reactorProjects", Collections.singletonList(project)); - setVariableValueToObject(mojo, "session", createMavenSession()); setVariableValueToObject(mojo, "skip", Boolean.TRUE); artifact = (InstallArtifactStub) project.getArtifact(); @@ -341,7 +388,10 @@ private String dotToSlashReplacer(String parameter) { private MavenSession createMavenSession() throws NoLocalRepositoryManagerException { MavenSession session = mock(MavenSession.class); DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession(); - repositorySession.setLocalRepositoryManager(new EnhancedLocalRepositoryManagerFactory() + repositorySession.setLocalRepositoryManager(new EnhancedLocalRepositoryManagerFactory( + new DefaultLocalPathComposer(), + new DefaultTrackingFileManager(), + new DefaultLocalPathPrefixComposerFactory()) .newInstance(repositorySession, new LocalRepository(LOCAL_REPO))); ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); buildingRequest.setRepositorySession(repositorySession); diff --git a/src/test/java/org/apache/maven/plugins/install/stubs/InstallArtifactStub.java b/src/test/java/org/apache/maven/plugins/install/stubs/InstallArtifactStub.java index d081382c..46d58c48 100644 --- a/src/test/java/org/apache/maven/plugins/install/stubs/InstallArtifactStub.java +++ b/src/test/java/org/apache/maven/plugins/install/stubs/InstallArtifactStub.java @@ -74,7 +74,7 @@ public String getExtension() { public void addMetadata(ArtifactMetadata metadata) { if (metadataMap == null) { - metadataMap = new HashMap(); + metadataMap = new HashMap<>(); } ArtifactMetadata m = metadataMap.get(metadata.getKey()); @@ -85,8 +85,8 @@ public void addMetadata(ArtifactMetadata metadata) { } } - public Collection getMetadataList() { - return metadataMap == null ? Collections.EMPTY_LIST : metadataMap.values(); + public Collection getMetadataList() { + return metadataMap == null ? Collections.emptyList() : metadataMap.values(); } public boolean isRelease() { diff --git a/src/test/java/org/apache/maven/plugins/install/stubs/InstallBomArtifactStub.java b/src/test/java/org/apache/maven/plugins/install/stubs/InstallBomArtifactStub.java new file mode 100644 index 00000000..7980b95a --- /dev/null +++ b/src/test/java/org/apache/maven/plugins/install/stubs/InstallBomArtifactStub.java @@ -0,0 +1,37 @@ +/* + * 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. + */ +package org.apache.maven.plugins.install.stubs; + +import org.apache.maven.artifact.handler.ArtifactHandler; +import org.apache.maven.artifact.handler.DefaultArtifactHandler; + +public class InstallBomArtifactStub extends InstallArtifactStub { + + public String getType() { + return "bom"; + } + + public ArtifactHandler getArtifactHandler() { + return new DefaultArtifactHandler() { + public String getExtension() { + return "pom"; + } + }; + } +} diff --git a/src/test/java/org/apache/maven/plugins/install/stubs/InstallPomArtifactStub.java b/src/test/java/org/apache/maven/plugins/install/stubs/InstallPomArtifactStub.java new file mode 100644 index 00000000..df6abd55 --- /dev/null +++ b/src/test/java/org/apache/maven/plugins/install/stubs/InstallPomArtifactStub.java @@ -0,0 +1,37 @@ +/* + * 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. + */ +package org.apache.maven.plugins.install.stubs; + +import org.apache.maven.artifact.handler.ArtifactHandler; +import org.apache.maven.artifact.handler.DefaultArtifactHandler; + +public class InstallPomArtifactStub extends InstallArtifactStub { + + public String getType() { + return "pom"; + } + + public ArtifactHandler getArtifactHandler() { + return new DefaultArtifactHandler() { + public String getExtension() { + return "pom"; + } + }; + } +} diff --git a/src/test/resources/unit/basic-install-test-packaging-bom/plugin-config.xml b/src/test/resources/unit/basic-install-test-packaging-bom/plugin-config.xml new file mode 100644 index 00000000..6c08d07c --- /dev/null +++ b/src/test/resources/unit/basic-install-test-packaging-bom/plugin-config.xml @@ -0,0 +1,35 @@ + + + + + + maven-install-plugin + + + ${basedir}/src/test/resources/unit/basic-install-test-packaging-bom/plugin-config.xml + bom + + + + + + + + diff --git a/src/test/resources/unit/basic-install-test-packaging-pom/plugin-config.xml b/src/test/resources/unit/basic-install-test-packaging-pom/plugin-config.xml index baf37e5e..647ddab7 100644 --- a/src/test/resources/unit/basic-install-test-packaging-pom/plugin-config.xml +++ b/src/test/resources/unit/basic-install-test-packaging-pom/plugin-config.xml @@ -25,7 +25,7 @@ under the License. ${basedir}/src/test/resources/unit/basic-install-test-packaging-pom/plugin-config.xml pom - +