diff --git a/.github/workflows/maven-verify.yml b/.github/workflows/maven-verify.yml index 4d67fdcf..e16c45b1 100644 --- a/.github/workflows/maven-verify.yml +++ b/.github/workflows/maven-verify.yml @@ -25,3 +25,6 @@ jobs: build: name: Verify uses: apache/maven-gh-actions-shared/.github/workflows/maven-verify.yml@v3 + with: + ff-maven: "4.0.0-alpha-4" # Maven version for fail-fast-build + maven-matrix: '[ "4.0.0-alpha-4" ]' \ No newline at end of file diff --git a/pom.xml b/pom.xml index 8e9fc70f..3328eaf1 100644 --- a/pom.xml +++ b/pom.xml @@ -28,7 +28,7 @@ maven-install-plugin - 3.1.1-SNAPSHOT + 4.0.0-SNAPSHOT maven-plugin Apache Maven Install Plugin @@ -72,9 +72,7 @@ 8 - 3.2.5 - - 1.0.0.v20140518 + 4.0.0-alpha-4 1.7.5 @@ -83,7 +81,8 @@ 3.10.1 3.1.0 3.3.0 - 3.7.0 + 3.8.1 + 4.0.0-alpha-1 3.3.0 3.2.1 3.0.0-M7 @@ -94,70 +93,26 @@ org.apache.maven - maven-plugin-api - ${mavenVersion} - provided - - - org.apache.maven - maven-artifact - ${mavenVersion} - provided - - - - org.apache.maven - maven-core + maven-api-core ${mavenVersion} provided org.apache.maven - maven-model + maven-api-model ${mavenVersion} provided - - org.eclipse.aether - aether-api - ${aetherVersion} - provided - - - org.eclipse.aether - aether-util - ${aetherVersion} - - compile - - - org.codehaus.plexus - plexus-utils - - - - - org.apache.maven.plugin-tools - maven-plugin-annotations - provided - - - junit - junit - 4.13.2 - test - org.apache.maven.plugin-testing maven-plugin-testing-harness - 3.3.0 + ${mavenPluginTestingHarnessVersion} test - org.apache.maven - maven-compat + maven-core ${mavenVersion} test @@ -167,26 +122,29 @@ 4.8.1 test - - org.slf4j - slf4j-api - ${slf4jVersion} - provided - org.slf4j slf4j-nop ${slf4jVersion} test - - org.eclipse.aether - aether-impl - ${aetherVersion} - test - + + + + org.apache.maven.plugins + maven-plugin-plugin + ${mavenPluginPluginVersion} + + + org.apache.maven.plugins + maven-surefire-plugin + ${mavenSurefirePluginVersion} + + + + run-its @@ -196,6 +154,7 @@ org.apache.maven.plugins maven-invoker-plugin + 3.5.0 ${project.build.directory}/it true @@ -212,6 +171,10 @@ clean install + + + ${https.protocols} + 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 86d87af9..b6554d69 100644 --- a/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java +++ b/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java @@ -22,59 +22,45 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; -import java.io.Reader; import java.io.Writer; import java.nio.file.Files; -import java.util.Enumeration; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; +import java.util.ArrayList; +import java.util.List; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.regex.Pattern; -import org.apache.maven.execution.MavenSession; -import org.apache.maven.model.Model; -import org.apache.maven.model.Parent; -import org.apache.maven.model.io.xpp3.MavenXpp3Reader; -import org.apache.maven.model.io.xpp3.MavenXpp3Writer; -import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.plugins.annotations.Component; -import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; -import org.codehaus.plexus.util.FileUtils; -import org.codehaus.plexus.util.IOUtil; -import org.codehaus.plexus.util.StringUtils; -import org.codehaus.plexus.util.xml.XmlStreamReader; -import org.codehaus.plexus.util.xml.XmlStreamWriter; -import org.codehaus.plexus.util.xml.pull.XmlPullParserException; -import org.eclipse.aether.DefaultRepositoryCache; -import org.eclipse.aether.DefaultRepositorySystemSession; -import org.eclipse.aether.RepositorySystem; -import org.eclipse.aether.RepositorySystemSession; -import org.eclipse.aether.artifact.Artifact; -import org.eclipse.aether.artifact.ArtifactType; -import org.eclipse.aether.artifact.DefaultArtifact; -import org.eclipse.aether.installation.InstallRequest; -import org.eclipse.aether.installation.InstallationException; -import org.eclipse.aether.repository.LocalRepository; -import org.eclipse.aether.repository.LocalRepositoryManager; -import org.eclipse.aether.util.artifact.SubArtifact; +import org.apache.maven.api.Artifact; +import org.apache.maven.api.Session; +import org.apache.maven.api.model.Model; +import org.apache.maven.api.model.Parent; +import org.apache.maven.api.plugin.Log; +import org.apache.maven.api.plugin.MojoException; +import org.apache.maven.api.plugin.annotations.Component; +import org.apache.maven.api.plugin.annotations.Mojo; +import org.apache.maven.api.plugin.annotations.Parameter; +import org.apache.maven.api.services.ArtifactInstaller; +import org.apache.maven.api.services.ArtifactInstallerException; +import org.apache.maven.api.services.ArtifactManager; +import org.apache.maven.api.services.xml.ModelXmlFactory; +import org.apache.maven.api.services.xml.XmlReaderException; /** * Installs a file in the local repository. - * - * @author Brett Porter */ -@Mojo(name = "install-file", requiresProject = false, aggregator = true, threadSafe = true) -public class InstallFileMojo extends AbstractMojo { - private static final String LS = System.getProperty("line.separator"); +@Mojo(name = "install-file", requiresProject = false, aggregator = true) +@SuppressWarnings("unused") +public class InstallFileMojo implements org.apache.maven.api.plugin.Mojo { + private static final String TAR = "tar."; + private static final String ILLEGAL_VERSION_CHARS = "\\/:\"<>|?*[](){},"; @Component - private RepositorySystem repositorySystem; + private Log log; - @Parameter(defaultValue = "${session}", required = true, readonly = true) - private MavenSession session; + @Component + private Session session; /** * GroupId of the artifact to be installed. Retrieved from POM file if one is specified or extracted from @@ -117,7 +103,7 @@ public class InstallFileMojo extends AbstractMojo { * The file to be installed in the local repository. */ @Parameter(property = "file", required = true) - private File file; + private Path file; /** * The bundled API docs for the artifact. @@ -125,7 +111,7 @@ public class InstallFileMojo extends AbstractMojo { * @since 2.3 */ @Parameter(property = "javadoc") - private File javadoc; + private Path javadoc; /** * The bundled sources for the artifact. @@ -133,7 +119,7 @@ public class InstallFileMojo extends AbstractMojo { * @since 2.3 */ @Parameter(property = "sources") - private File sources; + private Path sources; /** * Location of an existing POM file to be installed alongside the main artifact, given by the {@link #file} @@ -142,7 +128,7 @@ public class InstallFileMojo extends AbstractMojo { * @since 2.1 */ @Parameter(property = "pomFile") - private File pomFile; + private Path pomFile; /** * Generate a minimal POM for the artifact if none is supplied via the parameter {@link #pomFile}. Defaults to @@ -160,180 +146,145 @@ public class InstallFileMojo extends AbstractMojo { * @since 2.2 */ @Parameter(property = "localRepositoryPath") - private File localRepositoryPath; + private Path localRepositoryPath; @Override - public void execute() throws MojoExecutionException, MojoFailureException { - if (!file.exists()) { - String message = "The specified file '" + file.getPath() + "' does not exist"; - getLog().error(message); - throw new MojoFailureException(message); + public void execute() { + if (!Files.exists(file)) { + String message = "The specified file '" + file + "' does not exist"; + log.error(message); + throw new MojoException(message); } - RepositorySystemSession repositorySystemSession = session.getRepositorySession(); + Session session = this.session; + + List installableArtifacts = new ArrayList<>(); + + // Override the default local repository if (localRepositoryPath != null) { - // "clone" repository session and replace localRepository - DefaultRepositorySystemSession newSession = - new DefaultRepositorySystemSession(session.getRepositorySession()); - // Clear cache, since we're using a new local repository - newSession.setCache(new DefaultRepositoryCache()); - // keep same repositoryType - String contentType = newSession.getLocalRepository().getContentType(); - if ("enhanced".equals(contentType)) { - contentType = "default"; - } - LocalRepositoryManager localRepositoryManager = repositorySystem.newLocalRepositoryManager( - newSession, new LocalRepository(localRepositoryPath, contentType)); - newSession.setLocalRepositoryManager(localRepositoryManager); - repositorySystemSession = newSession; - getLog().debug("localRepoPath: " - + localRepositoryManager.getRepository().getBasedir()); - } + session = session.withLocalRepository(session.createLocalRepository(localRepositoryPath)); - File temporaryPom = null; + log.debug("localRepoPath: " + localRepositoryPath); + } + Path deployedPom; + Path temporaryPom = null; if (pomFile != null) { - processModel(readModel(pomFile)); + deployedPom = pomFile; + processModel(readModel(deployedPom)); } else { - temporaryPom = readingPomFromJarFile(); if (!Boolean.TRUE.equals(generatePom)) { - pomFile = temporaryPom; - getLog().debug("Using JAR embedded POM as pomFile"); + temporaryPom = readingPomFromJarFile(); + deployedPom = temporaryPom; + if (deployedPom != null) { + log.debug("Using JAR embedded POM as pomFile"); + } + } else { + deployedPom = null; } } if (groupId == null || artifactId == null || version == null || packaging == null) { - throw new MojoExecutionException("The artifact information is incomplete: 'groupId', 'artifactId', " + throw new MojoException("The artifact information is incomplete: 'groupId', 'artifactId', " + "'version' and 'packaging' are required."); } if (!isValidId(groupId) || !isValidId(artifactId) || !isValidVersion(version)) { - throw new MojoExecutionException("The artifact information is not valid: uses invalid characters."); + throw new MojoException("The artifact information is not valid: uses invalid characters."); } - InstallRequest installRequest = new InstallRequest(); - boolean isFilePom = classifier == null && "pom".equals(packaging); - if (!isFilePom) { - ArtifactType artifactType = - repositorySystemSession.getArtifactTypeRegistry().get(packaging); - if (artifactType != null - && StringUtils.isEmpty(classifier) - && !StringUtils.isEmpty(artifactType.getClassifier())) { - classifier = artifactType.getClassifier(); - } - } - Artifact mainArtifact = new DefaultArtifact( - groupId, artifactId, classifier, isFilePom ? "pom" : getExtension(file), version) - .setFile(file); - installRequest.addArtifact(mainArtifact); - - File artifactLocalFile = getLocalRepositoryFile(repositorySystemSession, mainArtifact); - File pomLocalFile = getPomLocalRepositoryFile(repositorySystemSession, mainArtifact); + Artifact artifact = session.createArtifact( + groupId, artifactId, version, classifier, isFilePom ? "pom" : getExtension(file), packaging); - if (file.equals(artifactLocalFile)) { - throw new MojoFailureException("Cannot install artifact. " + "Artifact is already in the local repository." - + LS + LS + "File in question is: " + file + LS); + if (file.equals(getLocalRepositoryFile(artifact))) { + throw new MojoException("Cannot install artifact. " + + "Artifact is already in the local repository.\n\nFile in question is: " + file + "\n"); } - if (!"pom".equals(packaging)) { - if (pomFile != null) { - installRequest.addArtifact(new SubArtifact(mainArtifact, "", "pom", pomFile)); + ArtifactManager artifactManager = session.getService(ArtifactManager.class); + artifactManager.setPath(artifact, file); + installableArtifacts.add(artifact); + + if (!isFilePom) { + Artifact pomArtifact = session.createArtifact(groupId, artifactId, version, null, "pom", null); + if (deployedPom != null) { + artifactManager.setPath(pomArtifact, deployedPom); + installableArtifacts.add(pomArtifact); } else { - if (Boolean.TRUE.equals(generatePom) || (generatePom == null && !pomLocalFile.exists())) { - temporaryPom = generatePomFile(); - getLog().debug("Installing generated POM"); - installRequest.addArtifact(new SubArtifact(mainArtifact, "", "pom", temporaryPom)); + temporaryPom = generatePomFile(); + deployedPom = temporaryPom; + artifactManager.setPath(pomArtifact, deployedPom); + if (Boolean.TRUE.equals(generatePom) + || (generatePom == null && !Files.exists(getLocalRepositoryFile(pomArtifact)))) { + log.debug("Installing generated POM"); + installableArtifacts.add(pomArtifact); } else if (generatePom == null) { - getLog().debug("Skipping installation of generated POM, already present in local repository"); + log.debug("Skipping installation of generated POM, already present in local repository"); } } } if (sources != null) { - installRequest.addArtifact(new SubArtifact(mainArtifact, "sources", "jar", sources)); + Artifact sourcesArtifact = session.createArtifact(groupId, artifactId, version, "sources", "jar", null); + artifactManager.setPath(sourcesArtifact, sources); + installableArtifacts.add(sourcesArtifact); } if (javadoc != null) { - installRequest.addArtifact(new SubArtifact(mainArtifact, "javadoc", "jar", javadoc)); + Artifact javadocArtifact = session.createArtifact(groupId, artifactId, version, "javadoc", "jar", null); + artifactManager.setPath(javadocArtifact, javadoc); + installableArtifacts.add(javadocArtifact); } try { - repositorySystem.install(repositorySystemSession, installRequest); - } catch (InstallationException e) { - throw new MojoExecutionException(e.getMessage(), e); + ArtifactInstaller artifactInstaller = session.getService(ArtifactInstaller.class); + artifactInstaller.install(session, installableArtifacts); + } catch (ArtifactInstallerException e) { + throw new MojoException(e.getMessage(), e); } finally { if (temporaryPom != null) { - // noinspection ResultOfMethodCallIgnored - temporaryPom.delete(); + try { + Files.deleteIfExists(temporaryPom); + } catch (IOException e) { + // ignore + } } } } - private File readingPomFromJarFile() throws MojoExecutionException { - File pomFile = null; - - JarFile jarFile = null; + private Path readingPomFromJarFile() { + Pattern pomEntry = Pattern.compile("META-INF/maven/.*/pom\\.xml"); try { - Pattern pomEntry = Pattern.compile("META-INF/maven/.*/pom\\.xml"); - - jarFile = new JarFile(file); - - Enumeration jarEntries = jarFile.entries(); - - while (jarEntries.hasMoreElements()) { - JarEntry entry = jarEntries.nextElement(); - - if (pomEntry.matcher(entry.getName()).matches()) { - getLog().debug("Loading " + entry.getName()); - - InputStream pomInputStream = null; - OutputStream pomOutputStream = null; - - try { - pomInputStream = jarFile.getInputStream(entry); - - String base = file.getName(); + try (JarFile jarFile = new JarFile(file.toFile())) { + JarEntry entry = jarFile.stream() + .filter(e -> pomEntry.matcher(e.getName()).matches()) + .findFirst() + .orElse(null); + if (entry != null) { + log.debug("Loading " + entry.getName()); + + try (InputStream pomInputStream = jarFile.getInputStream(entry)) { + String base = file.getFileName().toString(); if (base.indexOf('.') > 0) { base = base.substring(0, base.lastIndexOf('.')); } - pomFile = File.createTempFile(base, ".pom"); - - pomOutputStream = Files.newOutputStream(pomFile.toPath()); + Path pomFile = File.createTempFile(base, ".pom").toPath(); - IOUtil.copy(pomInputStream, pomOutputStream); - - pomOutputStream.close(); - pomOutputStream = null; - - pomInputStream.close(); - pomInputStream = null; + Files.copy(pomInputStream, pomFile, StandardCopyOption.REPLACE_EXISTING); processModel(readModel(pomFile)); - break; - } finally { - IOUtil.close(pomInputStream); - IOUtil.close(pomOutputStream); + return pomFile; } + } else { + log.info("pom.xml not found in " + file.getFileName()); } } - - if (pomFile == null) { - getLog().info("pom.xml not found in " + file.getName()); - } } catch (IOException e) { // ignore, artifact not packaged by Maven - } finally { - if (jarFile != null) { - try { - jarFile.close(); - } catch (IOException e) { - // we did our best - } - } } - return pomFile; + return null; } /** @@ -341,24 +292,19 @@ private File readingPomFromJarFile() throws MojoExecutionException { * * @param pomFile The path of the POM file to parse, must not be null. * @return The model from the POM file, never null. - * @throws MojoExecutionException If the POM could not be parsed. + * @throws MojoException If the POM could not be parsed. */ - private Model readModel(File pomFile) throws MojoExecutionException { - Reader reader = null; + private Model readModel(Path pomFile) throws MojoException { try { - reader = new XmlStreamReader(pomFile); - final Model model = new MavenXpp3Reader().read(reader); - reader.close(); - reader = null; - return model; + try (InputStream is = Files.newInputStream(pomFile)) { + return session.getService(ModelXmlFactory.class).read(is); + } } catch (FileNotFoundException e) { - throw new MojoExecutionException("File not found " + pomFile, e); + throw new MojoException("File not found " + pomFile, e); } catch (IOException e) { - throw new MojoExecutionException("Error reading POM " + pomFile, e); - } catch (XmlPullParserException e) { - throw new MojoExecutionException("Error parsing POM " + pomFile, e); - } finally { - IOUtil.close(reader); + throw new MojoException("Error reading POM " + pomFile, e); + } catch (XmlReaderException e) { + throw new MojoException("Error parsing POM " + pomFile, e); } } @@ -396,18 +342,14 @@ private void processModel(Model model) { * @return The generated model, never null. */ private Model generateModel() { - Model model = new Model(); - - model.setModelVersion("4.0.0"); - - model.setGroupId(groupId); - model.setArtifactId(artifactId); - model.setVersion(version); - model.setPackaging(packaging); - - model.setDescription("POM was created from install:install-file"); - - return model; + return Model.newBuilder() + .modelVersion("4.0.0") + .groupId(groupId) + .artifactId(artifactId) + .version(version) + .packaging(packaging) + .description("POM was created from install:install-file") + .build(); } /** @@ -415,25 +357,18 @@ private Model generateModel() { * the generated file when no longer needed. * * @return The path to the generated POM file, never null. - * @throws MojoExecutionException If the POM file could not be generated. + * @throws MojoException If the POM file could not be generated. */ - private File generatePomFile() throws MojoExecutionException { + private Path generatePomFile() throws MojoException { Model model = generateModel(); - - Writer writer = null; try { - File pomFile = File.createTempFile("mvninstall", ".pom"); - - writer = new XmlStreamWriter(pomFile); - new MavenXpp3Writer().write(writer, model); - writer.close(); - writer = null; - + Path pomFile = File.createTempFile("mvninstall", ".pom").toPath(); + try (Writer writer = Files.newBufferedWriter(pomFile)) { + session.getService(ModelXmlFactory.class).write(model, writer); + } return pomFile; } catch (IOException e) { - throw new MojoExecutionException("Error writing temporary POM file: " + e.getMessage(), e); - } finally { - IOUtil.close(writer); + throw new MojoException("Error writing temporary POM file: " + e.getMessage(), e); } } @@ -441,33 +376,21 @@ private File generatePomFile() throws MojoExecutionException { * Gets the path of the specified artifact within the local repository. Note that the returned path need not exist * (yet). */ - private File getLocalRepositoryFile(RepositorySystemSession session, Artifact artifact) { - String path = session.getLocalRepositoryManager().getPathForLocalArtifact(artifact); - return new File(session.getLocalRepository().getBasedir(), path); - } - - /** - * Gets the path of the specified artifact POM within the local repository. Note that the returned path need - * not exist (yet). - */ - private File getPomLocalRepositoryFile(RepositorySystemSession session, Artifact artifact) { - SubArtifact pomArtifact = new SubArtifact(artifact, "", "pom"); - String path = session.getLocalRepositoryManager().getPathForLocalArtifact(pomArtifact); - return new File(session.getLocalRepository().getBasedir(), path); + private Path getLocalRepositoryFile(Artifact artifact) { + return session.getPathForLocalArtifact(artifact); } - // these below should be shared (duplicated in m-install-p, m-deploy-p) - /** - * Specialization of {@link FileUtils#getExtension(String)} that honors various {@code tar.xxx} combinations. + * Get file extension, honoring various {@code tar.xxx} combinations. */ - private String getExtension(final File file) { - String filename = file.getName(); - if (filename.contains(".tar.")) { - return "tar." + FileUtils.getExtension(filename); - } else { - return FileUtils.getExtension(filename); + private String getExtension(final Path file) { + String filename = file.getFileName().toString(); + int lastDot = filename.lastIndexOf('.'); + if (lastDot > 0 && lastDot < filename.length() - 1) { + String ext = filename.substring(lastDot + 1); + return filename.regionMatches(lastDot + 1 - TAR.length(), TAR, 0, TAR.length()) ? TAR + ext : ext; } + return ""; } /** @@ -491,8 +414,6 @@ private boolean isValidId(String id) { return true; } - private static final String ILLEGAL_VERSION_CHARS = "\\/:\"<>|?*[](){},"; - /** * Returns {@code true} if passed in string is "valid Maven (simple. non range, expression, etc) version". */ 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 3646de86..93c705d8 100644 --- a/src/main/java/org/apache/maven/plugins/install/InstallMojo.java +++ b/src/main/java/org/apache/maven/plugins/install/InstallMojo.java @@ -18,50 +18,50 @@ */ package org.apache.maven.plugins.install; -import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.Map; - -import org.apache.maven.RepositoryUtils; -import org.apache.maven.execution.MavenSession; -import org.apache.maven.model.Plugin; -import org.apache.maven.model.PluginExecution; -import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.descriptor.PluginDescriptor; -import org.apache.maven.plugins.annotations.Component; -import org.apache.maven.plugins.annotations.LifecyclePhase; -import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; -import org.apache.maven.project.MavenProject; -import org.apache.maven.project.artifact.ProjectArtifact; -import org.eclipse.aether.RepositorySystem; -import org.eclipse.aether.installation.InstallRequest; -import org.eclipse.aether.installation.InstallationException; +import java.util.Objects; +import java.util.function.Predicate; +import java.util.stream.Collectors; + +import org.apache.maven.api.Artifact; +import org.apache.maven.api.MojoExecution; +import org.apache.maven.api.Project; +import org.apache.maven.api.Session; +import org.apache.maven.api.model.Plugin; +import org.apache.maven.api.plugin.Log; +import org.apache.maven.api.plugin.MojoException; +import org.apache.maven.api.plugin.annotations.Component; +import org.apache.maven.api.plugin.annotations.LifecyclePhase; +import org.apache.maven.api.plugin.annotations.Mojo; +import org.apache.maven.api.plugin.annotations.Parameter; +import org.apache.maven.api.services.ArtifactInstaller; +import org.apache.maven.api.services.ArtifactInstallerRequest; +import org.apache.maven.api.services.ArtifactManager; +import org.apache.maven.api.services.ProjectManager; /** * Installs the project's main artifact, and any other artifacts attached by other plugins in the lifecycle, to the * local repository. - * - * @author Emmanuel Venisse */ -@Mojo(name = "install", defaultPhase = LifecyclePhase.INSTALL, threadSafe = true) -public class InstallMojo extends AbstractMojo { +@SuppressWarnings("unused") +@Mojo(name = "install", defaultPhase = LifecyclePhase.INSTALL) +public class InstallMojo implements org.apache.maven.api.plugin.Mojo { @Component - private RepositorySystem repositorySystem; + private Log log; - @Parameter(defaultValue = "${session}", required = true, readonly = true) - private MavenSession session; - - @Parameter(defaultValue = "${project}", readonly = true, required = true) - private MavenProject project; + @Component + private Session session; - @Parameter(defaultValue = "${reactorProjects}", required = true, readonly = true) - private List reactorProjects; + @Component + private Project project; - @Parameter(defaultValue = "${plugin}", required = true, readonly = true) - private PluginDescriptor pluginDescriptor; + @Component + private MojoExecution mojoExecution; /** * Whether every project should be installed during its own install-phase or at the end of the multimodule build. If @@ -70,7 +70,7 @@ public class InstallMojo extends AbstractMojo { * * @since 2.5 */ - @Parameter(defaultValue = "false", property = "installAtEnd") + @Parameter(property = "installAtEnd", defaultValue = "false") private boolean installAtEnd; /** @@ -91,123 +91,127 @@ private enum State { private static final String INSTALL_PROCESSED_MARKER = InstallMojo.class.getName() + ".processed"; private void putState(State state) { - getPluginContext().put(INSTALL_PROCESSED_MARKER, state.name()); + session.getPluginContext(project).put(INSTALL_PROCESSED_MARKER, state.name()); + } + + private void putState(State state, ArtifactInstallerRequest request) { + session.getPluginContext(project).put(INSTALL_PROCESSED_MARKER, state.name()); + session.getPluginContext(project).put(ArtifactInstallerRequest.class.getName(), request); } - private State getState(MavenProject project) { - Map pluginContext = session.getPluginContext(pluginDescriptor, project); + private State getState(Project project) { + Map pluginContext = session.getPluginContext(project); return State.valueOf((String) pluginContext.get(INSTALL_PROCESSED_MARKER)); } - private boolean hasState(MavenProject project) { - Map pluginContext = session.getPluginContext(pluginDescriptor, project); + private boolean hasState(Project project) { + Map pluginContext = session.getPluginContext(project); return pluginContext.containsKey(INSTALL_PROCESSED_MARKER); } + private boolean usingPlugin(Project project) { + Plugin plugin = project.getBuild().getPluginsAsMap().get("org.apache.maven.plugins:maven-install-plugin"); + return plugin != null + && plugin.getExecutions().stream() + .anyMatch(e -> Objects.equals(e.getId(), mojoExecution.getExecutionId()) + && !"none".equals(e.getPhase())); + } + @Override - public void execute() throws MojoExecutionException { + public void execute() { if (skip) { - getLog().info("Skipping artifact installation"); + log.info("Skipping artifact installation"); putState(State.SKIPPED); } else { if (!installAtEnd) { - InstallRequest request = new InstallRequest(); - processProject(project, request); - installProject(request); + installProject(processProject(project)); putState(State.INSTALLED); } else { - getLog().info("Deferring install for " + project.getGroupId() + ":" + project.getArtifactId() + ":" + log.info("Deferring install for " + project.getGroupId() + ":" + project.getArtifactId() + ":" + project.getVersion() + " at end"); - putState(State.TO_BE_INSTALLED); + putState(State.TO_BE_INSTALLED, processProject(project)); } } - List allProjectsUsingPlugin = getAllProjectsUsingPlugin(); - - if (allProjectsMarked(allProjectsUsingPlugin)) { - InstallRequest request = new InstallRequest(); - for (MavenProject reactorProject : allProjectsUsingPlugin) { + List projectsUsingPlugin = + session.getProjects().stream().filter(this::usingPlugin).collect(Collectors.toList()); + if (allProjectsMarked(projectsUsingPlugin)) { + for (Project reactorProject : projectsUsingPlugin) { State state = getState(reactorProject); if (state == State.TO_BE_INSTALLED) { - processProject(reactorProject, request); + Map pluginContext = session.getPluginContext(reactorProject); + ArtifactInstallerRequest request = + (ArtifactInstallerRequest) pluginContext.get(ArtifactInstallerRequest.class.getName()); + installProject(request); } } - installProject(request); } } - private boolean allProjectsMarked(List allProjectsUsingPlugin) { - for (MavenProject reactorProject : allProjectsUsingPlugin) { - if (!hasState(reactorProject)) { - return false; - } - } - return true; + private boolean allProjectsMarked(List projectsUsingPlugin) { + return projectsUsingPlugin.stream().allMatch(this::hasState); } - private List getAllProjectsUsingPlugin() { - ArrayList result = new ArrayList<>(); - for (MavenProject reactorProject : reactorProjects) { - if (hasExecution(reactorProject.getPlugin("org.apache.maven.plugins:maven-install-plugin"))) { - result.add(reactorProject); - } - } - return result; - } - - private boolean hasExecution(Plugin plugin) { - if (plugin == null) { - return false; - } - - for (PluginExecution execution : plugin.getExecutions()) { - if (!execution.getGoals().isEmpty() && !"none".equalsIgnoreCase(execution.getPhase())) { - return true; - } - } - return false; - } - - private void installProject(InstallRequest request) throws MojoExecutionException { + private void installProject(ArtifactInstallerRequest request) { try { - repositorySystem.install(session.getRepositorySession(), request); - } catch (InstallationException e) { - throw new MojoExecutionException(e.getMessage(), e); + ArtifactInstaller artifactInstaller = session.getService(ArtifactInstaller.class); + artifactInstaller.install(request); + } catch (MojoException e) { + throw e; + } catch (Exception e) { + throw new MojoException(e.getMessage(), e); } } /** - * Processes passed in {@link MavenProject} and prepares content of {@link InstallRequest} out of it. + * Processes passed in {@link Project} and produces {@link ArtifactInstallerRequest} out of it. * - * @throws MojoExecutionException if project is badly set up. + * @throws IllegalArgumentException 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))); - } else { - throw new MojoExecutionException("The project POM could not be attached"); - } + private ArtifactInstallerRequest processProject(Project project) { + ArtifactManager artifactManager = session.getService(ArtifactManager.class); + ProjectManager projectManager = session.getService(ProjectManager.class); + Predicate isValidPath = + a -> artifactManager.getPath(a).filter(Files::isRegularFile).isPresent(); + + Artifact artifact = project.getArtifact(); + Collection attachedArtifacts = projectManager.getAttachedArtifacts(project); + Path pomPath = project.getPomPath().orElse(null); + + List installables = new ArrayList<>(); if (!"pom".equals(project.getPackaging())) { - org.apache.maven.artifact.Artifact mavenMainArtifact = project.getArtifact(); - if (isFile(mavenMainArtifact.getFile())) { - request.addArtifact(RepositoryUtils.toArtifact(mavenMainArtifact)); - } else if (!project.getAttachedArtifacts().isEmpty()) { - throw new MojoExecutionException("The packaging plugin for this project did not assign " + // pom + Artifact pomArtifact = + session.createArtifact(project.getGroupId(), project.getArtifactId(), project.getVersion(), "pom"); + artifactManager.setPath(pomArtifact, pomPath); + installables.add(pomArtifact); + // main artifact + if (!isValidPath.test(artifact) && !attachedArtifacts.isEmpty()) { + throw new MojoException("The packaging plugin for this project did not assign " + "a main file to the project but it has attachments. Change packaging to 'pom'."); - } else { - throw new MojoExecutionException( - "The packaging for this project did not assign " + "a file to the build artifact"); } + installables.add(artifact); + } else { + artifactManager.setPath(artifact, pomPath); + installables.add(artifact); } - for (org.apache.maven.artifact.Artifact attached : project.getAttachedArtifacts()) { - getLog().debug("Attaching for install: " + attached.getId()); - request.addArtifact(RepositoryUtils.toArtifact(attached)); + installables.addAll(attachedArtifacts); + for (Artifact installable : installables) { + if (!isValidPath.test(installable)) { + throw new MojoException("The packaging for this project did not assign " + + "a file to the attached artifact: " + artifact); + } } + + return ArtifactInstallerRequest.builder() + .session(session) + .artifacts(installables) + .build(); } - private boolean isFile(File file) { - return file != null && file.isFile(); + void setSkip(boolean skip) { + this.skip = skip; } } 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..78e4843a 100644 --- a/src/test/java/org/apache/maven/plugins/install/InstallFileMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/install/InstallFileMojoTest.java @@ -19,334 +19,286 @@ package org.apache.maven.plugins.install; import java.io.File; -import java.io.Reader; - -import org.apache.maven.execution.MavenSession; -import org.apache.maven.model.Model; -import org.apache.maven.model.io.xpp3.MavenXpp3Reader; -import org.apache.maven.plugin.testing.AbstractMojoTestCase; -import org.apache.maven.project.DefaultProjectBuildingRequest; -import org.apache.maven.project.ProjectBuildingRequest; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Consumer; + +import com.google.inject.Inject; +import com.google.inject.Provides; +import com.google.inject.Singleton; +import org.apache.maven.api.Artifact; +import org.apache.maven.api.LocalRepository; +import org.apache.maven.api.Session; +import org.apache.maven.api.model.Model; +import org.apache.maven.api.plugin.MojoException; +import org.apache.maven.api.plugin.testing.InjectMojo; +import org.apache.maven.api.plugin.testing.MojoTest; +import org.apache.maven.api.plugin.testing.stubs.ArtifactStub; +import org.apache.maven.api.plugin.testing.stubs.SessionStub; +import org.apache.maven.api.services.ArtifactInstaller; +import org.apache.maven.api.services.ArtifactInstallerRequest; +import org.apache.maven.api.services.ArtifactManager; +import org.apache.maven.api.services.xml.ModelXmlFactory; import org.codehaus.plexus.util.FileUtils; -import org.codehaus.plexus.util.xml.XmlStreamReader; -import org.eclipse.aether.DefaultRepositorySystemSession; -import org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManagerFactory; -import org.eclipse.aether.repository.LocalRepository; -import org.eclipse.aether.repository.NoLocalRepositoryManagerException; - -import static org.mockito.Mockito.mock; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.apache.maven.api.plugin.testing.MojoExtension.getVariableValueFromObject; +import static org.codehaus.plexus.testing.PlexusExtension.getBasedir; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.when; /** * @author Allan Ramirez */ -public class InstallFileMojoTest extends AbstractMojoTestCase { - private String groupId; +@MojoTest +public class InstallFileMojoTest { + private static final String LOCAL_REPO = "target/local-repo"; + private String groupId; private String artifactId; - private String version; - private String packaging; - private String classifier; + private Path file; - private File file; + @Inject + Session session; - private final String LOCAL_REPO = "target/local-repo/"; + @Inject + ArtifactManager artifactManager; - private final String SPECIFIC_LOCAL_REPO = "target/specific-local-repo/"; + @Inject + ArtifactInstaller artifactInstaller; + @BeforeEach public void setUp() throws Exception { - super.setUp(); - FileUtils.deleteDirectory(new File(getBasedir() + "/" + LOCAL_REPO)); - FileUtils.deleteDirectory(new File(getBasedir() + "/" + SPECIFIC_LOCAL_REPO)); } - public void testInstallFileFromLocalRepositoryToLocalRepositoryPath() throws Exception { - File localRepository = - new File(getBasedir(), "target/test-classes/unit/install-file-from-local-repository-test/target"); - - File testPom = new File(localRepository.getParentFile(), "plugin-config.xml"); - - InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom); - + @Test + @InjectMojo(goal = "install-file", pom = "classpath:/unit/install-file/basic-test.xml") + public void testInstallFileTestEnvironment(InstallFileMojo mojo) { assertNotNull(mojo); - - setVariableValueToObject(mojo, "session", createMavenSession(localRepository.getAbsolutePath())); - - File specificLocalRepositoryPath = new File(getBasedir() + "/" + SPECIFIC_LOCAL_REPO); - - setVariableValueToObject(mojo, "localRepositoryPath", specificLocalRepositoryPath); - - assignValuesForParameter(mojo); - - mojo.execute(); - - String localPath = getBasedir() + "/" + SPECIFIC_LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" - + artifactId + "-" + version; - - File installedArtifact = new File(localPath + "." + "jar"); - - assertTrue(installedArtifact.exists()); - - assertEquals( - FileUtils.getFiles(new File(SPECIFIC_LOCAL_REPO), null, null).toString(), - 5, - FileUtils.getFiles(new File(SPECIFIC_LOCAL_REPO), null, null).size()); } - public void testInstallFileWithLocalRepositoryPath() throws Exception { - File testPom = - new File(getBasedir(), "target/test-classes/unit/install-file-with-checksum/" + "plugin-config.xml"); - - InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom); - + @Test + @InjectMojo(goal = "install-file", pom = "classpath:/unit/install-file/basic-test.xml") + public void testBasicInstallFile(InstallFileMojo mojo) throws Exception { assertNotNull(mojo); - - setVariableValueToObject(mojo, "session", createMavenSession(LOCAL_REPO)); - - File specificLocalRepositoryPath = new File(getBasedir() + "/" + SPECIFIC_LOCAL_REPO); - - setVariableValueToObject(mojo, "localRepositoryPath", specificLocalRepositoryPath); - assignValuesForParameter(mojo); mojo.execute(); - String localPath = getBasedir() + "/" + SPECIFIC_LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" - + artifactId + "-" + version; - - File installedArtifact = new File(localPath + "." + "jar"); - - assertTrue(installedArtifact.exists()); + ArtifactInstallerRequest request = execute(mojo); + assertNotNull(request); + Set artifacts = new HashSet<>(request.getArtifacts()); + Artifact pom = getArtifact(null, "pom"); + Artifact jar = getArtifact(null, "jar"); + assertEquals(new HashSet<>(Arrays.asList(pom, jar)), artifacts); + assertFileExists(artifactManager.getPath(jar).orElse(null)); + assertFileExists(artifactManager.getPath(jar).orElse(null)); assertEquals( - FileUtils.getFiles(new File(SPECIFIC_LOCAL_REPO), null, null).toString(), - 5, - FileUtils.getFiles(new File(SPECIFIC_LOCAL_REPO), null, null).size()); - } - - public void testInstallFileTestEnvironment() throws Exception { - File testPom = new File(getBasedir(), "target/test-classes/unit/install-file-basic-test/plugin-config.xml"); - - InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom); - - setVariableValueToObject(mojo, "session", createMavenSession(LOCAL_REPO)); - - assertNotNull(mojo); + LOCAL_REPO, + request.getSession().getLocalRepository().getPath().toString().replace(File.separator, "/")); } - public void testBasicInstallFile() throws Exception { - File testPom = new File(getBasedir(), "target/test-classes/unit/install-file-basic-test/plugin-config.xml"); - - InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom); - + @Test + @InjectMojo(goal = "install-file", pom = "classpath:/unit/install-file/file-absent.xml") + public void testFileDoesNotExists(InstallFileMojo mojo) throws Exception { assertNotNull(mojo); - - setVariableValueToObject(mojo, "session", createMavenSession(LOCAL_REPO)); - assignValuesForParameter(mojo); - mojo.execute(); - - File installedArtifact = new File( - getBasedir(), - LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version + "." - + packaging); - - assertTrue(installedArtifact.exists()); - - assertEquals(5, FileUtils.getFiles(new File(LOCAL_REPO), null, null).size()); + assertThrows(MojoException.class, mojo::execute); } - public void testInstallFileWithClassifier() throws Exception { - File testPom = - new File(getBasedir(), "target/test-classes/unit/install-file-with-classifier/plugin-config.xml"); - - InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom); - + @Test + @InjectMojo(goal = "install-file", pom = "classpath:/unit/install-file/with-classifier.xml") + public void testInstallFileWithClassifier(InstallFileMojo mojo) throws Exception { assertNotNull(mojo); - - setVariableValueToObject(mojo, "session", createMavenSession(LOCAL_REPO)); - assignValuesForParameter(mojo); - assertNotNull(classifier); - mojo.execute(); - - File installedArtifact = new File( - getBasedir(), - LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version + "-" - + classifier + "." + packaging); - - assertTrue(installedArtifact.exists()); + ArtifactInstallerRequest request = execute(mojo); - assertEquals(5, FileUtils.getFiles(new File(LOCAL_REPO), null, null).size()); + assertNotNull(request); + Set artifacts = new HashSet<>(request.getArtifacts()); + Artifact pom = getArtifact(null, "pom"); + Artifact sources = getArtifact("sources", "jar"); + assertEquals(new HashSet<>(Arrays.asList(pom, sources)), artifacts); + // pom file does not exists, as it should have been deleted after the installation + assertFileNotExists(artifactManager.getPath(pom).get()); + assertFileExists(artifactManager.getPath(sources).get()); + assertEquals( + LOCAL_REPO, + request.getSession().getLocalRepository().getPath().toString().replace(File.separator, "/")); } - public void testInstallFileWithGeneratePom() throws Exception { - File testPom = - new File(getBasedir(), "target/test-classes/unit/install-file-test-generatePom/plugin-config.xml"); - - InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom); - + @Test + @InjectMojo(goal = "install-file", pom = "classpath:/unit/install-file/test-generatePom.xml") + public void testInstallFileWithGeneratePom(InstallFileMojo mojo) throws Exception { assertNotNull(mojo); - - setVariableValueToObject(mojo, "session", createMavenSession(LOCAL_REPO)); - assignValuesForParameter(mojo); - - mojo.execute(); - - File installedArtifact = new File( - getBasedir(), - LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version + "." - + packaging); - assertTrue((Boolean) getVariableValueFromObject(mojo, "generatePom")); - assertTrue(installedArtifact.exists()); - - File installedPom = new File( - getBasedir(), - LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version + "." - + "pom"); - - try (Reader reader = new XmlStreamReader(installedPom)) { - Model model = new MavenXpp3Reader().read(reader); - - assertEquals("4.0.0", model.getModelVersion()); - - assertEquals((String) getVariableValueFromObject(mojo, "groupId"), model.getGroupId()); - - assertEquals(artifactId, model.getArtifactId()); + AtomicReference model = new AtomicReference<>(); + ArtifactInstallerRequest request = execute(mojo, air -> model.set(readModel(getArtifact(null, "pom")))); + + assertNotNull(request); + Set artifacts = new HashSet<>(request.getArtifacts()); + Artifact pom = getArtifact(null, "pom"); + Artifact jar = getArtifact(null, "jar"); + assertEquals(new HashSet<>(Arrays.asList(pom, jar)), artifacts); + assertEquals("4.0.0", model.get().getModelVersion()); + assertEquals(getVariableValueFromObject(mojo, "groupId"), model.get().getGroupId()); + assertEquals(artifactId, model.get().getArtifactId()); + assertEquals(version, model.get().getVersion()); + assertNotNull(artifactManager.getPath(jar).orElse(null)); + assertEquals( + LOCAL_REPO, + request.getSession().getLocalRepository().getPath().toString().replace(File.separator, "/")); + } - assertEquals(version, model.getVersion()); + private Model readModel(Artifact pom) { + try { + Path pomPath = artifactManager.getPath(pom).orElse(null); + assertNotNull(pomPath); + return session.getService(ModelXmlFactory.class).read(pomPath); + } catch (Exception e) { + throw new IllegalStateException(e); } - - assertEquals(5, FileUtils.getFiles(new File(LOCAL_REPO), null, null).size()); } - public void testInstallFileWithPomFile() throws Exception { - File testPom = - new File(getBasedir(), "target/test-classes/unit/install-file-with-pomFile-test/plugin-config.xml"); - - InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom); - + @Test + @InjectMojo(goal = "install-file", pom = "classpath:/unit/install-file/with-pomFile-test.xml") + public void testInstallFileWithPomFile(InstallFileMojo mojo) throws Exception { assertNotNull(mojo); - - setVariableValueToObject(mojo, "session", createMavenSession(LOCAL_REPO)); - assignValuesForParameter(mojo); + Path pomFile = (Path) getVariableValueFromObject(mojo, "pomFile"); - mojo.execute(); - - File pomFile = (File) getVariableValueFromObject(mojo, "pomFile"); - - assertTrue(pomFile.exists()); - - File installedArtifact = new File( - getBasedir(), - LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version + "." - + packaging); - - assertTrue(installedArtifact.exists()); - - File installedPom = new File( - getBasedir(), - LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version + "." - + "pom"); + ArtifactInstallerRequest request = execute(mojo); - assertTrue(installedPom.exists()); - - assertEquals(5, FileUtils.getFiles(new File(LOCAL_REPO), null, null).size()); + assertNotNull(request); + Set artifacts = new HashSet<>(request.getArtifacts()); + Artifact pom = getArtifact(null, "pom"); + Artifact jar = getArtifact(null, "jar"); + assertEquals(new HashSet<>(Arrays.asList(pom, jar)), artifacts); + assertEquals(pomFile, artifactManager.getPath(pom).orElse(null)); + assertNotNull(artifactManager.getPath(jar).orElse(null)); + assertEquals( + LOCAL_REPO, + request.getSession().getLocalRepository().getPath().toString().replace(File.separator, "/")); } - public void testInstallFileWithPomAsPackaging() throws Exception { - File testPom = new File( - getBasedir(), "target/test-classes/unit/install-file-with-pom-as-packaging/" + "plugin-config.xml"); - - InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom); - + @Test + @InjectMojo(goal = "install-file", pom = "classpath:/unit/install-file/with-pom-as-packaging.xml") + public void testInstallFileWithPomAsPackaging(InstallFileMojo mojo) throws Exception { assertNotNull(mojo); - - setVariableValueToObject(mojo, "session", createMavenSession(LOCAL_REPO)); - assignValuesForParameter(mojo); - - assertTrue(file.exists()); - + assertTrue(Files.exists(file)); assertEquals("pom", packaging); - mojo.execute(); - - File installedPom = new File( - getBasedir(), - LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version + "." - + "pom"); - - assertTrue(installedPom.exists()); + ArtifactInstallerRequest request = execute(mojo); - assertEquals(4, FileUtils.getFiles(new File(LOCAL_REPO), null, null).size()); + assertNotNull(request); + Set artifacts = new HashSet<>(request.getArtifacts()); + Artifact pom = getArtifact(null, "pom"); + assertEquals(Collections.singleton(pom), artifacts); } - public void testInstallFile() throws Exception { - File testPom = - new File(getBasedir(), "target/test-classes/unit/install-file-with-checksum/" + "plugin-config.xml"); - - InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom); - + @Test + @InjectMojo(goal = "install-file", pom = "classpath:/unit/install-file/with-checksum.xml") + public void testInstallFile(InstallFileMojo mojo) throws Exception { assertNotNull(mojo); - - setVariableValueToObject(mojo, "session", createMavenSession(LOCAL_REPO)); - assignValuesForParameter(mojo); - mojo.execute(); - - String localPath = getBasedir() + "/" + LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" - + artifactId + "-" + version; - - File installedArtifact = new File(localPath + "." + "jar"); - - assertTrue(installedArtifact.exists()); + ArtifactInstallerRequest request = execute(mojo); + assertNotNull(request); + Set artifacts = new HashSet<>(request.getArtifacts()); + Artifact pom = getArtifact(null, "pom"); + Artifact jar = getArtifact(null, "jar"); + assertEquals(new HashSet<>(Arrays.asList(pom, jar)), artifacts); assertEquals( - FileUtils.getFiles(new File(LOCAL_REPO), null, null).toString(), - 5, - FileUtils.getFiles(new File(LOCAL_REPO), null, null).size()); + LOCAL_REPO, + request.getSession().getLocalRepository().getPath().toString().replace(File.separator, "/")); } private void assignValuesForParameter(Object obj) throws Exception { - this.groupId = dotToSlashReplacer((String) getVariableValueFromObject(obj, "groupId")); - + this.groupId = (String) getVariableValueFromObject(obj, "groupId"); this.artifactId = (String) getVariableValueFromObject(obj, "artifactId"); - this.version = (String) getVariableValueFromObject(obj, "version"); - this.packaging = (String) getVariableValueFromObject(obj, "packaging"); - this.classifier = (String) getVariableValueFromObject(obj, "classifier"); + this.file = (Path) getVariableValueFromObject(obj, "file"); + } + + private ArtifactStub getArtifact(String classifier, String extension) { + return new ArtifactStub(groupId, artifactId, classifier != null ? classifier : "", version, extension); + } + + private ArtifactInstallerRequest execute(InstallFileMojo mojo) { + return execute(mojo, null); + } - this.file = (File) getVariableValueFromObject(obj, "file"); + private ArtifactInstallerRequest execute(InstallFileMojo mojo, Consumer consumer) { + AtomicReference request = new AtomicReference<>(); + doAnswer(iom -> { + ArtifactInstallerRequest req = iom.getArgument(0, ArtifactInstallerRequest.class); + request.set(req); + if (consumer != null) { + consumer.accept(req); + } + return null; + }) + .when(artifactInstaller) + .install(any(ArtifactInstallerRequest.class)); + mojo.execute(); + return request.get(); + } + + private void assertFileExists(Path path) { + assertTrue(path != null && Files.exists(path), () -> path + " should exists"); } - private String dotToSlashReplacer(String parameter) { - return parameter.replace('.', '/'); + private void assertFileNotExists(Path path) { + assertFalse(path != null && Files.exists(path), () -> path + " should not exists"); } - private MavenSession createMavenSession(String localRepositoryBaseDir) throws NoLocalRepositoryManagerException { - MavenSession session = mock(MavenSession.class); - DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession(); - repositorySession.setLocalRepositoryManager(new EnhancedLocalRepositoryManagerFactory() - .newInstance(repositorySession, new LocalRepository(localRepositoryBaseDir))); - ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - buildingRequest.setRepositorySession(repositorySession); - when(session.getProjectBuildingRequest()).thenReturn(buildingRequest); - when(session.getRepositorySession()).thenReturn(repositorySession); + @Provides + @Singleton + @SuppressWarnings("unused") + private Session createMavenSession() { + Session session = SessionStub.getMockSession(LOCAL_REPO); + when(session.withLocalRepository(any())).thenAnswer(iom -> { + LocalRepository localRepository = iom.getArgument(0, LocalRepository.class); + Session mockSession = SessionStub.getMockSession(localRepository); + when(mockSession.getService(ArtifactInstaller.class)) + .thenAnswer(iom2 -> session.getService(ArtifactInstaller.class)); + return mockSession; + }); return session; } + + @Provides + private ArtifactInstaller createArtifactInstaller(Session session) { + return session.getService(ArtifactInstaller.class); + } + + @Provides + private ArtifactManager createArtifactManager(Session session) { + return session.getService(ArtifactManager.class); + } } 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..f20e2a38 100644 --- a/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java @@ -18,348 +18,197 @@ */ package org.apache.maven.plugins.install; +import javax.inject.Named; + import java.io.File; +import java.nio.file.Paths; +import java.util.Arrays; import java.util.Collections; +import java.util.HashSet; import java.util.List; -import java.util.concurrent.ConcurrentHashMap; - -import org.apache.maven.artifact.Artifact; -import org.apache.maven.execution.MavenSession; -import org.apache.maven.model.Build; -import org.apache.maven.model.Plugin; -import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.descriptor.PluginDescriptor; -import org.apache.maven.plugin.testing.AbstractMojoTestCase; -import org.apache.maven.plugins.install.stubs.AttachedArtifactStub0; -import org.apache.maven.plugins.install.stubs.InstallArtifactStub; -import org.apache.maven.project.DefaultProjectBuildingRequest; -import org.apache.maven.project.MavenProject; -import org.apache.maven.project.ProjectBuildingRequest; +import java.util.Set; +import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Consumer; + +import com.google.inject.Inject; +import com.google.inject.Provides; +import com.google.inject.Singleton; +import org.apache.maven.api.Artifact; +import org.apache.maven.api.Project; +import org.apache.maven.api.Session; +import org.apache.maven.api.plugin.Mojo; +import org.apache.maven.api.plugin.MojoException; +import org.apache.maven.api.plugin.testing.InjectMojo; +import org.apache.maven.api.plugin.testing.MojoTest; +import org.apache.maven.api.plugin.testing.stubs.ArtifactStub; +import org.apache.maven.api.plugin.testing.stubs.SessionStub; +import org.apache.maven.api.services.ArtifactInstaller; +import org.apache.maven.api.services.ArtifactInstallerRequest; +import org.apache.maven.api.services.ArtifactManager; +import org.apache.maven.api.services.ProjectManager; import org.codehaus.plexus.util.FileUtils; -import org.eclipse.aether.DefaultRepositorySystemSession; -import org.eclipse.aether.artifact.DefaultArtifact; -import org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManagerFactory; -import org.eclipse.aether.repository.LocalRepository; -import org.eclipse.aether.repository.NoLocalRepositoryManagerException; - +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.apache.maven.api.plugin.testing.MojoExtension.getBasedir; +import static org.apache.maven.api.plugin.testing.MojoExtension.getVariableValueFromObject; +import static org.apache.maven.api.plugin.testing.MojoExtension.setVariableValueToObject; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.doAnswer; /** * @author Allan Ramirez */ -public class InstallMojoTest extends AbstractMojoTestCase { +@MojoTest +public class InstallMojoTest { - InstallArtifactStub artifact; + private static final String LOCAL_REPO = "target/local-repo/"; - private final String LOCAL_REPO = "target/local-repo/"; + @Inject + Session session; - public void setUp() throws Exception { - super.setUp(); + ArtifactInstaller artifactInstaller; + ArtifactManager artifactManager; + ProjectManager projectManager; + @BeforeEach + public void setUp() throws Exception { FileUtils.deleteDirectory(new File(getBasedir() + "/" + LOCAL_REPO)); + artifactInstaller = session.getService(ArtifactInstaller.class); + artifactManager = session.getService(ArtifactManager.class); + projectManager = session.getService(ProjectManager.class); } - public void testInstallTestEnvironment() throws Exception { - File testPom = new File(getBasedir(), "target/test-classes/unit/basic-install-test/plugin-config.xml"); - - AbstractMojo mojo = (AbstractMojo) lookupMojo("install", testPom); - + @Test + @InjectMojo(goal = "install", pom = "classpath:/unit/basic-install/test.xml") + public void testInstallTestEnvironment(InstallMojo mojo) { assertNotNull(mojo); } - public void testBasicInstall() throws Exception { - File testPom = new File(getBasedir(), "target/test-classes/unit/basic-install-test/plugin-config.xml"); - - AbstractMojo mojo = (AbstractMojo) lookupMojo("install", testPom); - + @Test + @InjectMojo(goal = "install", pom = "classpath:/unit/basic-install/test.xml") + public void testBasicInstall(InstallMojo mojo) throws Exception { assertNotNull(mojo); - - File file = new File( - getBasedir(), - "target/test-classes/unit/basic-install-test/target/" + "maven-install-test-1.0-SNAPSHOT.jar"); - - MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project"); - updateMavenProject(project); - - setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>()); - setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor()); - setVariableValueToObject(mojo, "reactorProjects", Collections.singletonList(project)); - setVariableValueToObject(mojo, "session", createMavenSession()); - - artifact = (InstallArtifactStub) project.getArtifact(); - - artifact.setFile(file); - - mojo.execute(); - - String groupId = dotToSlashReplacer(artifact.getGroupId()); - - File installedArtifact = new File( - getBasedir(), - LOCAL_REPO + groupId + "/" + artifact.getArtifactId() + "/" + artifact.getVersion() + "/" - + artifact.getArtifactId() + "-" + artifact.getVersion() + "." - + artifact.getArtifactHandler().getExtension()); - - assertTrue(installedArtifact.exists()); - - assertEquals(5, FileUtils.getFiles(new File(LOCAL_REPO), null, null).size()); + Project project = (Project) getVariableValueFromObject(mojo, "project"); + artifactManager.setPath( + project.getArtifact(), + Paths.get(getBasedir(), "target/test-classes/unit/basic-install/maven-install-test-1.0-SNAPSHOT.jar")); + + ArtifactInstallerRequest request = execute(mojo); + + assertNotNull(request); + Set artifacts = new HashSet<>(request.getArtifacts()); + assertEquals( + new HashSet<>(Arrays.asList( + new ArtifactStub("org.apache.maven.test", "maven-install-test", "", "1.0-SNAPSHOT", "jar"), + new ArtifactStub("org.apache.maven.test", "maven-install-test", "", "1.0-SNAPSHOT", "pom"))), + artifacts); } - public void testBasicInstallWithAttachedArtifacts() throws Exception { - File testPom = new File( - getBasedir(), - "target/test-classes/unit/basic-install-test-with-attached-artifacts/" + "plugin-config.xml"); - - AbstractMojo mojo = (AbstractMojo) lookupMojo("install", testPom); - + @Test + @InjectMojo(goal = "install", pom = "classpath:/unit/basic-install/test.xml") + public void testBasicInstallWithAttachedArtifacts(InstallMojo mojo) throws Exception { assertNotNull(mojo); - - MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project"); - updateMavenProject(project); - - setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>()); - setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor()); - setVariableValueToObject(mojo, "reactorProjects", Collections.singletonList(project)); - setVariableValueToObject(mojo, "session", createMavenSession()); - - List attachedArtifacts = project.getAttachedArtifacts(); - - mojo.execute(); - - String packaging = project.getPackaging(); - - String groupId; - - for (Object attachedArtifact1 : attachedArtifacts) { - AttachedArtifactStub0 attachedArtifact = (AttachedArtifactStub0) attachedArtifact1; - - groupId = dotToSlashReplacer(attachedArtifact.getGroupId()); - - File installedArtifact = new File( - getBasedir(), - LOCAL_REPO + groupId + "/" + attachedArtifact.getArtifactId() - + "/" + attachedArtifact.getVersion() + "/" + attachedArtifact.getArtifactId() - + "-" + attachedArtifact.getVersion() + "." + packaging); - - assertTrue(installedArtifact.getPath() + " does not exist", installedArtifact.exists()); - } - - assertEquals(13, FileUtils.getFiles(new File(LOCAL_REPO), null, null).size()); + Project project = (Project) getVariableValueFromObject(mojo, "project"); + projectManager.attachArtifact( + project, + new ArtifactStub("org.apache.maven.test", "attached-artifact-test", "", "1.0-SNAPSHOT", "jar"), + Paths.get( + getBasedir(), + "target/test-classes/unit/basic-install/attached-artifact-test-1.0-SNAPSHOT.jar")); + artifactManager.setPath( + project.getArtifact(), + Paths.get(getBasedir(), "target/test-classes/unit/basic-install/maven-install-test-1.0-SNAPSHOT.jar")); + + ArtifactInstallerRequest request = execute(mojo); + + assertNotNull(request); + Set artifacts = new HashSet<>(request.getArtifacts()); + assertEquals( + new HashSet<>(Arrays.asList( + new ArtifactStub("org.apache.maven.test", "maven-install-test", "", "1.0-SNAPSHOT", "jar"), + new ArtifactStub("org.apache.maven.test", "maven-install-test", "", "1.0-SNAPSHOT", "pom"), + new ArtifactStub( + "org.apache.maven.test", "attached-artifact-test", "", "1.0-SNAPSHOT", "jar"))), + artifacts); } - public void testUpdateReleaseParamSetToTrue() throws Exception { - File testPom = new File(getBasedir(), "target/test-classes/unit/configured-install-test/plugin-config.xml"); - - AbstractMojo mojo = (AbstractMojo) lookupMojo("install", testPom); - - assertNotNull(mojo); - - File file = new File( - getBasedir(), - "target/test-classes/unit/configured-install-test/target/" + "maven-install-test-1.0-SNAPSHOT.jar"); - - MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project"); - updateMavenProject(project); - - setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>()); - setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor()); - setVariableValueToObject(mojo, "reactorProjects", Collections.singletonList(project)); - setVariableValueToObject(mojo, "session", createMavenSession()); - - artifact = (InstallArtifactStub) project.getArtifact(); - - artifact.setFile(file); - - mojo.execute(); - - // assertTrue( artifact.isRelease() ); - - assertEquals(5, FileUtils.getFiles(new File(LOCAL_REPO), null, null).size()); - } - - public void testInstallIfArtifactFileIsNull() throws Exception { - File testPom = new File(getBasedir(), "target/test-classes/unit/basic-install-test/plugin-config.xml"); - - AbstractMojo mojo = (AbstractMojo) lookupMojo("install", testPom); - + @Test + @InjectMojo(goal = "install", pom = "classpath:/unit/basic-install/test.xml") + public void testInstallIfArtifactFileIsNull(InstallMojo mojo) throws Exception { assertNotNull(mojo); + Project project = (Project) getVariableValueFromObject(mojo, "project"); + assertFalse(artifactManager.getPath(project.getArtifact()).isPresent()); - MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project"); - updateMavenProject(project); - - setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>()); - setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor()); - setVariableValueToObject(mojo, "reactorProjects", Collections.singletonList(project)); - setVariableValueToObject(mojo, "session", createMavenSession()); - - artifact = (InstallArtifactStub) project.getArtifact(); - - artifact.setFile(null); - - assertNull(artifact.getFile()); - - try { - mojo.execute(); - - fail("Did not throw mojo execution exception"); - } catch (MojoExecutionException e) { - // expected - } - - assertFalse(new File(LOCAL_REPO).exists()); + assertThrows(MojoException.class, mojo::execute, "Did not throw mojo execution exception"); } - public void testInstallIfPackagingIsPom() throws Exception { - File testPom = new File( - getBasedir(), "target/test-classes/unit/basic-install-test-packaging-pom/" + "plugin-config.xml"); - - AbstractMojo mojo = (AbstractMojo) lookupMojo("install", testPom); - + @Test + @InjectMojo(goal = "install", pom = "classpath:/unit/basic-install/packaging-pom.xml") + public void testInstallIfPackagingIsPom(InstallMojo mojo) throws Exception { assertNotNull(mojo); - - MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project"); - updateMavenProject(project); - - setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>()); - setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor()); - setVariableValueToObject(mojo, "reactorProjects", Collections.singletonList(project)); - setVariableValueToObject(mojo, "session", createMavenSession()); - + Project project = (Project) getVariableValueFromObject(mojo, "project"); String packaging = project.getPackaging(); - assertEquals("pom", packaging); + artifactManager.setPath(project.getArtifact(), project.getPomPath().get()); - artifact = (InstallArtifactStub) project.getArtifact(); + ArtifactInstallerRequest request = execute(mojo); - 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()); + assertNotNull(request); + Set artifacts = new HashSet<>(request.getArtifacts()); + assertEquals( + new HashSet<>(Arrays.asList( + new ArtifactStub("org.apache.maven.test", "maven-install-test", "", "1.0-SNAPSHOT", "pom"))), + artifacts); } - public void testBasicInstallAndCreate() throws Exception { - File testPom = new File(getBasedir(), "target/test-classes/unit/basic-install-checksum/plugin-config.xml"); - - AbstractMojo mojo = (AbstractMojo) lookupMojo("install", testPom); - + @Test + @InjectMojo(goal = "install", pom = "classpath:/unit/basic-install/test.xml") + public void testSkip(InstallMojo mojo) throws Exception { assertNotNull(mojo); + setVariableValueToObject(mojo, "session", this.session); + Project project = (Project) getVariableValueFromObject(mojo, "project"); + mojo.setSkip(true); - File file = new File(getBasedir(), "target/test-classes/unit/basic-install-checksum/" + "maven-test-jar.jar"); - - MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project"); - MavenSession mavenSession = createMavenSession(); - updateMavenProject(project); - - setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>()); - setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor()); - setVariableValueToObject(mojo, "reactorProjects", Collections.singletonList(project)); - setVariableValueToObject(mojo, "session", mavenSession); - - artifact = (InstallArtifactStub) project.getArtifact(); - - artifact.setFile(file); - - mojo.execute(); - - File pom = new File( - new File(LOCAL_REPO), - mavenSession - .getRepositorySession() - .getLocalRepositoryManager() - .getPathForLocalArtifact(new DefaultArtifact( - artifact.getGroupId(), artifact.getArtifactId(), "pom", artifact.getVersion()))); - - assertTrue(pom.exists()); - - String groupId = dotToSlashReplacer(artifact.getGroupId()); - String packaging = project.getPackaging(); - String localPath = getBasedir() + "/" + LOCAL_REPO + groupId + "/" + artifact.getArtifactId() + "/" - + artifact.getVersion() + "/" + artifact.getArtifactId() + "-" + artifact.getVersion(); - - File installedArtifact = new File(localPath + "." + packaging); - - assertTrue(installedArtifact.exists()); - - assertEquals(5, FileUtils.getFiles(new File(LOCAL_REPO), null, null).size()); + assertNull(execute(mojo)); } - public void testSkip() throws Exception { - File testPom = new File(getBasedir(), "target/test-classes/unit/basic-install-test/plugin-config.xml"); - - InstallMojo mojo = (InstallMojo) lookupMojo("install", testPom); - - assertNotNull(mojo); - - File file = new File( - getBasedir(), - "target/test-classes/unit/basic-install-test/target/" + "maven-install-test-1.0-SNAPSHOT.jar"); - - MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project"); - updateMavenProject(project); - - 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(); - - artifact.setFile(file); - - mojo.execute(); - - String groupId = dotToSlashReplacer(artifact.getGroupId()); - - String packaging = project.getPackaging(); - - File installedArtifact = new File( - getBasedir(), - LOCAL_REPO + groupId + "/" + artifact.getArtifactId() + "/" + artifact.getVersion() + "/" - + artifact.getArtifactId() + "-" + artifact.getVersion() + "." + packaging); - - assertFalse(installedArtifact.exists()); - - assertFalse(new File(LOCAL_REPO).exists()); + @Provides + @Singleton + @SuppressWarnings("unused") + private Session createSession() { + return SessionStub.getMockSession(LOCAL_REPO); } - private String dotToSlashReplacer(String parameter) { - return parameter.replace('.', '/'); + @Provides + @Singleton + @Named("dummy.reactorProjects") + private List getDummyReactorProjects() { + return Collections.emptyList(); } - private MavenSession createMavenSession() throws NoLocalRepositoryManagerException { - MavenSession session = mock(MavenSession.class); - DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession(); - repositorySession.setLocalRepositoryManager(new EnhancedLocalRepositoryManagerFactory() - .newInstance(repositorySession, new LocalRepository(LOCAL_REPO))); - ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - buildingRequest.setRepositorySession(repositorySession); - when(session.getProjectBuildingRequest()).thenReturn(buildingRequest); - when(session.getRepositorySession()).thenReturn(repositorySession); - when(session.getPluginContext(any(PluginDescriptor.class), any(MavenProject.class))) - .thenReturn(new ConcurrentHashMap()); - return session; + private ArtifactInstallerRequest execute(Mojo mojo) { + return execute(mojo, null); } - private void updateMavenProject(MavenProject project) { - project.setGroupId(project.getArtifact().getGroupId()); - project.setArtifactId(project.getArtifact().getArtifactId()); - project.setVersion(project.getArtifact().getVersion()); - - Plugin plugin = new Plugin(); - plugin.setArtifactId("maven-install-plugin"); - project.setBuild(new Build()); - project.getBuild().addPlugin(plugin); + private ArtifactInstallerRequest execute(Mojo mojo, Consumer consumer) { + AtomicReference request = new AtomicReference<>(); + doAnswer(iom -> { + ArtifactInstallerRequest req = iom.getArgument(0, ArtifactInstallerRequest.class); + request.set(req); + if (consumer != null) { + consumer.accept(req); + } + return null; + }) + .when(artifactInstaller) + .install(any(ArtifactInstallerRequest.class)); + mojo.execute(); + return request.get(); } } diff --git a/src/test/java/org/apache/maven/plugins/install/stubs/AttachedArtifactStub0.java b/src/test/java/org/apache/maven/plugins/install/stubs/AttachedArtifactStub0.java deleted file mode 100644 index 53f30b4e..00000000 --- a/src/test/java/org/apache/maven/plugins/install/stubs/AttachedArtifactStub0.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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 java.io.File; - -public class AttachedArtifactStub0 extends InstallArtifactStub { - public String getArtifactId() { - return "attached-artifact-test-0"; - } - - public File getFile() { - return new File( - System.getProperty("basedir"), - "target/test-classes/unit/basic-install-test-with-attached-artifacts/" - + "target/maven-install-test-1.0-SNAPSHOT.jar"); - } -} diff --git a/src/test/java/org/apache/maven/plugins/install/stubs/AttachedArtifactStub1.java b/src/test/java/org/apache/maven/plugins/install/stubs/AttachedArtifactStub1.java deleted file mode 100644 index ad869b8c..00000000 --- a/src/test/java/org/apache/maven/plugins/install/stubs/AttachedArtifactStub1.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * 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; - -public class AttachedArtifactStub1 extends AttachedArtifactStub0 { - public String getArtifactId() { - return "attached-artifact-test-1"; - } -} 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 deleted file mode 100644 index d081382c..00000000 --- a/src/test/java/org/apache/maven/plugins/install/stubs/InstallArtifactStub.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * 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 java.io.File; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -import org.apache.maven.artifact.handler.ArtifactHandler; -import org.apache.maven.artifact.handler.DefaultArtifactHandler; -import org.apache.maven.artifact.metadata.ArtifactMetadata; -import org.apache.maven.plugin.testing.stubs.ArtifactStub; - -public class InstallArtifactStub extends ArtifactStub { - private Map metadataMap; - - private File file; - - private boolean release; - - public String getArtifactId() { - return "maven-install-test"; - } - - public String getGroupId() { - return "org.apache.maven.test"; - } - - public String getVersion() { - return "1.0-SNAPSHOT"; - } - - public String getBaseVersion() { - return getVersion(); - } - - public void setFile(File file) { - this.file = file; - } - - public File getFile() { - return file; - } - - public String getType() { - return "jar"; - } - - public ArtifactHandler getArtifactHandler() { - return new DefaultArtifactHandler() { - public String getExtension() { - return "jar"; - } - }; - } - - public void addMetadata(ArtifactMetadata metadata) { - if (metadataMap == null) { - metadataMap = new HashMap(); - } - - ArtifactMetadata m = metadataMap.get(metadata.getKey()); - if (m != null) { - m.merge(metadata); - } else { - metadataMap.put(metadata.getKey(), metadata); - } - } - - public Collection getMetadataList() { - return metadataMap == null ? Collections.EMPTY_LIST : metadataMap.values(); - } - - public boolean isRelease() { - return release; - } - - public void setRelease(boolean release) { - this.release = release; - } -} diff --git a/src/test/resources/unit/basic-install-checksum/maven-test-jar.jar b/src/test/resources/unit/basic-install-checksum/maven-test-jar.jar deleted file mode 100644 index 5be54536..00000000 --- a/src/test/resources/unit/basic-install-checksum/maven-test-jar.jar +++ /dev/null @@ -1 +0,0 @@ -this is just a test jar \ No newline at end of file diff --git a/src/test/resources/unit/basic-install-checksum/plugin-config.xml b/src/test/resources/unit/basic-install-checksum/plugin-config.xml deleted file mode 100644 index 6b4b56c6..00000000 --- a/src/test/resources/unit/basic-install-checksum/plugin-config.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - - - - maven-install-plugin - - - ${basedir}/src/test/resources/unit/basic-install-checksum/plugin-config.xml - jar - - - - - - - - diff --git a/src/test/resources/unit/basic-install-test-with-attached-artifacts/plugin-config.xml b/src/test/resources/unit/basic-install-test-with-attached-artifacts/plugin-config.xml deleted file mode 100644 index 30113ec9..00000000 --- a/src/test/resources/unit/basic-install-test-with-attached-artifacts/plugin-config.xml +++ /dev/null @@ -1,42 +0,0 @@ - - - - - - maven-install-plugin - - - ${basedir}/src/test/resources/unit/basic-install-test-with-attached-artifacts/plugin-config.xml - jar - - ${basedir}/src/test/resources/unit/basic-install-test-with-attached-artifacts/plugin-config.xml - - - - - - - - - - - diff --git a/src/test/resources/unit/basic-install/attached-artifact-test-1.0-SNAPSHOT.jar b/src/test/resources/unit/basic-install/attached-artifact-test-1.0-SNAPSHOT.jar new file mode 100644 index 00000000..6f5f2f81 --- /dev/null +++ b/src/test/resources/unit/basic-install/attached-artifact-test-1.0-SNAPSHOT.jar @@ -0,0 +1 @@ +This is not an actual jar \ No newline at end of file diff --git a/src/test/resources/unit/basic-install-test-with-attached-artifacts/target/maven-install-test-1.0-SNAPSHOT.jar b/src/test/resources/unit/basic-install/maven-install-test-1.0-SNAPSHOT.jar similarity index 100% rename from src/test/resources/unit/basic-install-test-with-attached-artifacts/target/maven-install-test-1.0-SNAPSHOT.jar rename to src/test/resources/unit/basic-install/maven-install-test-1.0-SNAPSHOT.jar diff --git a/src/test/resources/unit/basic-install-test-packaging-pom/plugin-config.xml b/src/test/resources/unit/basic-install/packaging-pom.xml similarity index 60% rename from src/test/resources/unit/basic-install-test-packaging-pom/plugin-config.xml rename to src/test/resources/unit/basic-install/packaging-pom.xml index baf37e5e..b19564fc 100644 --- a/src/test/resources/unit/basic-install-test-packaging-pom/plugin-config.xml +++ b/src/test/resources/unit/basic-install/packaging-pom.xml @@ -22,11 +22,18 @@ under the License. maven-install-plugin - - ${basedir}/src/test/resources/unit/basic-install-test-packaging-pom/plugin-config.xml + + ${basedir}/src/test/resources/unit/basic-install/packaging-pom.xml + org.apache.maven.test + maven-install-test + 1.0-SNAPSHOT pom - - + + org.apache.maven.test + maven-install-test + 1.0-SNAPSHOT + pom + diff --git a/src/test/resources/unit/basic-install-test/plugin-config.xml b/src/test/resources/unit/basic-install/test.xml similarity index 61% rename from src/test/resources/unit/basic-install-test/plugin-config.xml rename to src/test/resources/unit/basic-install/test.xml index c95dccc0..398d9d07 100644 --- a/src/test/resources/unit/basic-install-test/plugin-config.xml +++ b/src/test/resources/unit/basic-install/test.xml @@ -22,11 +22,18 @@ under the License. maven-install-plugin - - ${basedir}/src/test/resources/unit/basic-install-test/plugin-config.xml + + ${basedir}/src/test/resources/unit/basic-install/test.xml + org.apache.maven.test + maven-install-test + 1.0-SNAPSHOT jar - - + + org.apache.maven.test + maven-install-test + 1.0-SNAPSHOT + jar + diff --git a/src/test/resources/unit/configured-install-test/plugin-config.xml b/src/test/resources/unit/configured-install-test/plugin-config.xml deleted file mode 100644 index 2cc19ace..00000000 --- a/src/test/resources/unit/configured-install-test/plugin-config.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - - - - maven-install-plugin - - - ${basedir}/src/test/resources/unit/configured-install-test/plugin-config.xml - jar - - - - - - - - diff --git a/src/test/resources/unit/configured-install-test/target/maven-install-test-1.0-SNAPSHOT.jar b/src/test/resources/unit/configured-install-test/target/maven-install-test-1.0-SNAPSHOT.jar deleted file mode 100644 index 4a0e834e..00000000 Binary files a/src/test/resources/unit/configured-install-test/target/maven-install-test-1.0-SNAPSHOT.jar and /dev/null differ diff --git a/src/test/resources/unit/install-file-basic-test/target/maven-install-test-1.0-SNAPSHOT.jar b/src/test/resources/unit/install-file-basic-test/target/maven-install-test-1.0-SNAPSHOT.jar deleted file mode 100644 index 4a0e834e..00000000 Binary files a/src/test/resources/unit/install-file-basic-test/target/maven-install-test-1.0-SNAPSHOT.jar and /dev/null differ diff --git a/src/test/resources/unit/install-file-test-generatePom/target/maven-install-test-1.0-SNAPSHOT.jar b/src/test/resources/unit/install-file-test-generatePom/target/maven-install-test-1.0-SNAPSHOT.jar deleted file mode 100644 index 9c1e1116..00000000 Binary files a/src/test/resources/unit/install-file-test-generatePom/target/maven-install-test-1.0-SNAPSHOT.jar and /dev/null differ diff --git a/src/test/resources/unit/install-file-with-checksum/maven-test-jar.jar b/src/test/resources/unit/install-file-with-checksum/maven-test-jar.jar deleted file mode 100644 index 5be54536..00000000 --- a/src/test/resources/unit/install-file-with-checksum/maven-test-jar.jar +++ /dev/null @@ -1 +0,0 @@ -this is just a test jar \ No newline at end of file diff --git a/src/test/resources/unit/install-file-with-classifier/target/maven-install-test-1.0-SNAPSHOT.jar b/src/test/resources/unit/install-file-with-classifier/target/maven-install-test-1.0-SNAPSHOT.jar deleted file mode 100644 index 4a0e834e..00000000 Binary files a/src/test/resources/unit/install-file-with-classifier/target/maven-install-test-1.0-SNAPSHOT.jar and /dev/null differ diff --git a/src/test/resources/unit/install-file-with-pomFile-test/target/maven-install-test-1.0-SNAPSHOT.jar b/src/test/resources/unit/install-file-with-pomFile-test/target/maven-install-test-1.0-SNAPSHOT.jar deleted file mode 100644 index 4a0e834e..00000000 Binary files a/src/test/resources/unit/install-file-with-pomFile-test/target/maven-install-test-1.0-SNAPSHOT.jar and /dev/null differ diff --git a/src/test/resources/unit/install-file-basic-test/plugin-config.xml b/src/test/resources/unit/install-file/basic-test.xml similarity index 94% rename from src/test/resources/unit/install-file-basic-test/plugin-config.xml rename to src/test/resources/unit/install-file/basic-test.xml index 40523434..ac19871f 100644 --- a/src/test/resources/unit/install-file-basic-test/plugin-config.xml +++ b/src/test/resources/unit/install-file/basic-test.xml @@ -26,8 +26,7 @@ under the License. maven-install-file-test 1.0-SNAPSHOT jar - ${basedir}/target/test-classes/unit/install-file-basic-test/target/maven-install-test-1.0-SNAPSHOT.jar - + ${basedir}/target/test-classes/unit/install-file/maven-install-test-1.0-SNAPSHOT.jar diff --git a/src/test/resources/unit/install-file/file-absent.xml b/src/test/resources/unit/install-file/file-absent.xml new file mode 100644 index 00000000..3f8d4ebd --- /dev/null +++ b/src/test/resources/unit/install-file/file-absent.xml @@ -0,0 +1,34 @@ + + + + + + maven-install-plugin + + org.apache.maven.test + maven-install-file-test + 1.0-SNAPSHOT + jar + ${basedir}/target/test-classes/unit/install-file/files-does-not-exists.jar + + + + + diff --git a/src/test/resources/unit/basic-install-test/target/maven-install-test-1.0-SNAPSHOT.jar b/src/test/resources/unit/install-file/maven-install-test-1.0-SNAPSHOT.jar similarity index 100% rename from src/test/resources/unit/basic-install-test/target/maven-install-test-1.0-SNAPSHOT.jar rename to src/test/resources/unit/install-file/maven-install-test-1.0-SNAPSHOT.jar diff --git a/src/test/resources/unit/install-file-test-generatePom/plugin-config.xml b/src/test/resources/unit/install-file/test-generatePom.xml similarity index 86% rename from src/test/resources/unit/install-file-test-generatePom/plugin-config.xml rename to src/test/resources/unit/install-file/test-generatePom.xml index f6624f6c..267fc748 100644 --- a/src/test/resources/unit/install-file-test-generatePom/plugin-config.xml +++ b/src/test/resources/unit/install-file/test-generatePom.xml @@ -23,11 +23,11 @@ under the License. maven-install-plugin org.apache.maven.test - maven-install-file-test + maven-install-test 1.0-SNAPSHOT jar - ${basedir}/src/test/resources/unit/install-file-test-generatePom/target/maven-install-test-1.0-SNAPSHOT.jar + ${basedir}/src/test/resources/unit/install-file/maven-install-test-1.0-SNAPSHOT.jar true diff --git a/src/test/resources/unit/install-file-with-checksum/plugin-config.xml b/src/test/resources/unit/install-file/with-checksum.xml similarity index 91% rename from src/test/resources/unit/install-file-with-checksum/plugin-config.xml rename to src/test/resources/unit/install-file/with-checksum.xml index 91655ef7..fa4147d5 100644 --- a/src/test/resources/unit/install-file-with-checksum/plugin-config.xml +++ b/src/test/resources/unit/install-file/with-checksum.xml @@ -26,7 +26,7 @@ under the License. maven-install-file-test 1.0-SNAPSHOT jar - ${basedir}/src/test/resources/unit/install-file-with-checksum/maven-test-jar.jar + ${basedir}/src/test/resources/unit/install-file/maven-install-test-1.0-SNAPSHOT.jar true diff --git a/src/test/resources/unit/install-file-with-classifier/plugin-config.xml b/src/test/resources/unit/install-file/with-classifier.xml similarity index 95% rename from src/test/resources/unit/install-file-with-classifier/plugin-config.xml rename to src/test/resources/unit/install-file/with-classifier.xml index 8d6a18e6..da2240d1 100644 --- a/src/test/resources/unit/install-file-with-classifier/plugin-config.xml +++ b/src/test/resources/unit/install-file/with-classifier.xml @@ -27,7 +27,7 @@ under the License. 1.0-SNAPSHOT jar sources - ${basedir}/target/test-classes/unit/install-file-with-classifier/target/maven-install-test-1.0-SNAPSHOT.jar + ${basedir}/target/test-classes/unit/install-file/maven-install-test-1.0-SNAPSHOT.jar diff --git a/src/test/resources/unit/install-file-with-pom-as-packaging/plugin-config.xml b/src/test/resources/unit/install-file/with-pom-as-packaging.xml similarity index 91% rename from src/test/resources/unit/install-file-with-pom-as-packaging/plugin-config.xml rename to src/test/resources/unit/install-file/with-pom-as-packaging.xml index 4ec759d2..daa30e02 100644 --- a/src/test/resources/unit/install-file-with-pom-as-packaging/plugin-config.xml +++ b/src/test/resources/unit/install-file/with-pom-as-packaging.xml @@ -26,7 +26,7 @@ under the License. maven-install-file-test 1.0-SNAPSHOT pom - ${basedir}/src/test/resources/unit/install-file-with-pomFile-test/plugin-config.xml + ${basedir}/src/test/resources/unit/install-file/with-pomFile-test.xml diff --git a/src/test/resources/unit/install-file-with-pomFile-test/plugin-config.xml b/src/test/resources/unit/install-file/with-pomFile-test.xml similarity index 90% rename from src/test/resources/unit/install-file-with-pomFile-test/plugin-config.xml rename to src/test/resources/unit/install-file/with-pomFile-test.xml index fc7c1080..6ce977b4 100644 Binary files a/src/test/resources/unit/install-file-with-pomFile-test/plugin-config.xml and b/src/test/resources/unit/install-file/with-pomFile-test.xml differ