Skip to content

Commit

Permalink
[MINSTALL-197] Update to parent 41, cleanup (#61)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cstamas committed Jan 12, 2024
1 parent 30d2b53 commit 3ebb448
Show file tree
Hide file tree
Showing 10 changed files with 258 additions and 56 deletions.
61 changes: 39 additions & 22 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<parent>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugins</artifactId>
<version>39</version>
<version>41</version>
<relativePath />
</parent>

Expand All @@ -46,7 +46,7 @@
</contributors>

<prerequisites>
<maven>${mavenVersion}</maven>
<maven>3.2.5</maven>
</prerequisites>

<scm>
Expand All @@ -72,21 +72,21 @@

<properties>
<javaVersion>8</javaVersion>
<mavenVersion>3.2.5</mavenVersion>
<mavenVersion>3.9.6</mavenVersion>
<!-- Maven bound -->
<aetherVersion>1.0.0.v20140518</aetherVersion>
<resolverVersion>1.9.18</resolverVersion>
<!-- Maven bound -->
<slf4jVersion>1.7.5</slf4jVersion>
<slf4jVersion>1.7.36</slf4jVersion>

<!-- plugin versions used in IT tests -->
<mavenAntrunPluginVersion>3.1.0</mavenAntrunPluginVersion>
<mavenCompilerPluginVersion>3.10.1</mavenCompilerPluginVersion>
<mavenEnforcerPluginVersion>3.1.0</mavenEnforcerPluginVersion>
<mavenJarPluginVersion>3.3.0</mavenJarPluginVersion>
<mavenPluginPluginVersion>3.7.0</mavenPluginPluginVersion>
<mavenResourcesPluginVersion>3.3.0</mavenResourcesPluginVersion>
<mavenSourcePluginVersion>3.2.1</mavenSourcePluginVersion>
<mavenSurefirePluginVersion>3.0.0-M7</mavenSurefirePluginVersion>
<mavenAntrunPluginVersion>${version.maven-antrun-plugin}</mavenAntrunPluginVersion>
<mavenCompilerPluginVersion>${version.maven-compiler-plugin}</mavenCompilerPluginVersion>
<mavenEnforcerPluginVersion>${version.maven-enforcer-plugin}</mavenEnforcerPluginVersion>
<mavenJarPluginVersion>${version.maven-jar-plugin}</mavenJarPluginVersion>
<mavenPluginPluginVersion>${version.maven-plugin-tools}</mavenPluginPluginVersion>
<mavenResourcesPluginVersion>${version.maven-resources-plugin}</mavenResourcesPluginVersion>
<mavenSourcePluginVersion>${version.maven-source-plugin}</mavenSourcePluginVersion>
<mavenSurefirePluginVersion>${version.maven-surefire}</mavenSurefirePluginVersion>

<project.build.outputTimestamp>2023-03-21T14:31:18Z</project.build.outputTimestamp>
</properties>
Expand Down Expand Up @@ -118,22 +118,26 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-api</artifactId>
<version>${aetherVersion}</version>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-api</artifactId>
<version>${resolverVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-util</artifactId>
<version>${aetherVersion}</version>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-util</artifactId>
<version>${resolverVersion}</version>
<!-- To work in Maven versions older than 3.9.0 -->
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-xml</artifactId>
</dependency>

<!-- dependencies to annotations -->
<dependency>
Expand Down Expand Up @@ -180,13 +184,26 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-impl</artifactId>
<version>${aetherVersion}</version>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-impl</artifactId>
<version>${resolverVersion}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<proc>none</proc>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>run-its</id>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
40 changes: 30 additions & 10 deletions src/main/java/org/apache/maven/plugins/install/InstallMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<MavenProject> reactorProjects;

@Parameter(defaultValue = "${plugin}", required = true, readonly = true)
private PluginDescriptor pluginDescriptor;

Expand Down Expand Up @@ -157,7 +156,9 @@ private boolean allProjectsMarked(List<MavenProject> allProjectsUsingPlugin) {
rp -> hasExecution(rp.getPlugin("org.apache.maven.plugins:maven-install-plugin"));

private List<MavenProject> getAllProjectsUsingPlugin() {
return reactorProjects.stream().filter(hasMavenInstallPluginExecution).collect(Collectors.toList());
return session.getProjects().stream()
.filter(hasMavenInstallPluginExecution)
.collect(Collectors.toList());
}

private final Predicate<PluginExecution> havingGoals = pe -> !pe.getGoals().isEmpty();
Expand Down Expand Up @@ -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("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
80 changes: 65 additions & 15 deletions src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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<Artifact> attachedArtifacts = project.getAttachedArtifacts();

Expand Down Expand Up @@ -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();

Expand All @@ -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();

Expand Down Expand Up @@ -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();

Expand All @@ -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");

Expand All @@ -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();

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 3ebb448

Please sign in to comment.