copiedArtifacts;
-
- /**
- * Extra dependencies that need to be installed on the local repository.
+ * Extra dependencies that need to be installed on the local repository.
+ *
* Format:
- *
*
* groupId:artifactId:version:type:classifier
*
- *
+ *
* Examples:
- *
*
* org.apache.maven.plugins:maven-clean-plugin:2.4:maven-plugin
* org.apache.maven.plugins:maven-clean-plugin:2.4:jar:javadoc
*
- *
+ *
* If the type is 'maven-plugin' the plugin will try to resolve the artifact using plugin remote repositories,
* instead of using artifact remote repositories.
+ *
+ * NOTICE all dependencies will be resolved with transitive dependencies in runtime
scope.
*
* @since 1.6
*/
@@ -159,19 +142,12 @@ public class InstallMojo extends AbstractMojo {
private String[] extraArtifacts;
/**
+ * Scope to resolve project artifacts.
+ *
+ * @since 3.5.0
*/
- @Component
- private DependencyResolver resolver;
-
- /**
- * if the local repository is not used as test repo, the parameter can force get artifacts from local repo
- * if available instead of download the artifacts again.
- * @since 3.2.1
- */
- @Parameter(property = "invoker.useLocalRepository", defaultValue = "false")
- private boolean useLocalRepository;
-
- private ProjectBuildingRequest projectBuildingRequest;
+ @Parameter(property = "invoker.install.scope", defaultValue = "runtime")
+ private String scope;
/**
* Performs this mojo's tasks.
@@ -184,377 +160,246 @@ public void execute() throws MojoExecutionException {
return;
}
- createTestRepository();
-
- installedArtifacts = new HashSet<>();
- copiedArtifacts = new HashSet<>();
+ Collection resolvedArtifacts = new ArrayList<>();
- installProjectDependencies(project, reactorProjects);
- installProjectParents(project);
- installProjectArtifacts(project);
+ try {
- installExtraArtifacts(extraArtifacts);
+ resolveProjectArtifacts(resolvedArtifacts);
+ resolveProjectPoms(project, resolvedArtifacts);
+ resolveProjectDependencies(resolvedArtifacts);
+ resolveExtraArtifacts(resolvedArtifacts);
+ installArtifacts(resolvedArtifacts);
+
+ } catch (DependencyResolutionException
+ | InstallationException
+ | ArtifactDescriptorException
+ | ArtifactResolutionException e) {
+ throw new MojoExecutionException(e.getMessage(), e);
+ }
}
- /**
- * Creates the local repository for the integration tests. If the user specified a custom repository location, the
- * custom repository will have the same identifier, layout and policies as the real local repository. That means
- * apart from the location, the custom repository will be indistinguishable from the real repository such that its
- * usage is transparent to the integration tests.
- *
- * @throws MojoExecutionException If the repository could not be created.
- */
- private void createTestRepository() throws MojoExecutionException {
+ private void resolveProjectArtifacts(Collection resolvedArtifacts) {
- if (!localRepositoryPath.exists() && !localRepositoryPath.mkdirs()) {
- throw new MojoExecutionException("Failed to create directory: " + localRepositoryPath);
+ // pom packaging doesn't have a main artifact
+ if (project.getArtifact() != null && project.getArtifact().getFile() != null) {
+ resolvedArtifacts.add(RepositoryUtils.toArtifact(project.getArtifact()));
}
- projectBuildingRequest =
- repositoryManager.setLocalRepositoryBasedir(session.getProjectBuildingRequest(), localRepositoryPath);
+
+ resolvedArtifacts.addAll(project.getAttachedArtifacts().stream()
+ .map(RepositoryUtils::toArtifact)
+ .collect(Collectors.toList()));
}
- /**
- * Installs the specified artifact to the local repository. Note: This method should only be used for artifacts that
- * originate from the current (reactor) build. Artifacts that have been grabbed from the user's local repository
- * should be installed to the test repository via {@link #copyArtifact(File, Artifact)}.
- *
- * @param file The file associated with the artifact, must not be null
. This is in most cases the value
- * of artifact.getFile()
with the exception of the main artifact from a project with
- * packaging "pom". Projects with packaging "pom" have no main artifact file. They have however artifact
- * metadata (e.g. site descriptors) which needs to be installed.
- * @param artifact The artifact to install, must not be null
.
- * @throws MojoExecutionException If the artifact could not be installed (e.g. has no associated file).
- */
- private void installArtifact(File file, Artifact artifact) throws MojoExecutionException {
- try {
- if (file == null) {
- throw new IllegalStateException("Artifact has no associated file: " + artifact.getId());
- }
- if (!file.isFile()) {
- throw new IllegalStateException("Artifact is not fully assembled: " + file);
- }
+ private void resolveProjectPoms(MavenProject project, Collection resolvedArtifacts)
+ throws ArtifactResolutionException {
- if (installedArtifacts.add(artifact.getId())) {
- artifact.setFile(file);
- installer.install(projectBuildingRequest, localRepositoryPath, Collections.singletonList(artifact));
- } else {
- getLog().debug("Not re-installing " + artifact + ", " + file);
- }
- } catch (Exception e) {
- throw new MojoExecutionException("Failed to install artifact: " + artifact, e);
+ if (project == null) {
+ return;
+ }
+
+ Artifact projectPom = RepositoryUtils.toArtifact(new ProjectArtifact(project));
+ if (projectPom.getFile() != null) {
+ resolvedArtifacts.add(projectPom);
+ } else {
+ Artifact artifact = resolveArtifact(projectPom, project.getRemoteProjectRepositories());
+ resolvedArtifacts.add(artifact);
}
+ resolveProjectPoms(project.getParent(), resolvedArtifacts);
}
- /**
- * Installs the specified artifact to the local repository. This method serves basically the same purpose as
- * {@link #installArtifact(File, Artifact)} but is meant for artifacts that have been resolved
- * from the user's local repository (and not the current build outputs). The subtle difference here is that
- * artifacts from the repository have already undergone transformations and these manipulations should not be redone
- * by the artifact installer. For this reason, this method performs plain copy operations to install the artifacts.
- *
- * @param file The file associated with the artifact, must not be null
.
- * @param artifact The artifact to install, must not be null
.
- * @throws MojoExecutionException If the artifact could not be installed (e.g. has no associated file).
- */
- private void copyArtifact(File file, Artifact artifact) throws MojoExecutionException {
- try {
- if (file == null) {
- throw new IllegalStateException("Artifact has no associated file: " + artifact.getId());
- }
- if (!file.isFile()) {
- throw new IllegalStateException("Artifact is not fully assembled: " + file);
- }
+ private void resolveProjectDependencies(Collection resolvedArtifacts)
+ throws ArtifactResolutionException, MojoExecutionException, DependencyResolutionException {
- if (copiedArtifacts.add(artifact.getId())) {
- File destination = new File(
- localRepositoryPath,
- repositoryManager.getPathForLocalArtifact(projectBuildingRequest, artifact));
+ DependencyFilter classpathFilter = DependencyFilterUtils.classpathFilter(scope);
- getLog().debug("Installing " + file + " to " + destination);
+ ArtifactTypeRegistry artifactTypeRegistry =
+ session.getRepositorySession().getArtifactTypeRegistry();
- copyFileIfDifferent(file, destination);
+ List managedDependencies = Optional.ofNullable(project.getDependencyManagement())
+ .map(DependencyManagement::getDependencies)
+ .orElseGet(Collections::emptyList)
+ .stream()
+ .map(d -> RepositoryUtils.toDependency(d, artifactTypeRegistry))
+ .collect(Collectors.toList());
- MetadataUtils.createMetadata(destination, artifact);
- } else {
- getLog().debug("Not re-installing " + artifact + ", " + file);
- }
- } catch (Exception e) {
- throw new MojoExecutionException("Failed to stage artifact: " + artifact, e);
- }
- }
+ List dependencies = project.getDependencies().stream()
+ .map(d -> RepositoryUtils.toDependency(d, artifactTypeRegistry))
+ .collect(Collectors.toList());
- private void copyFileIfDifferent(File src, File dst) throws IOException {
- if (src.lastModified() != dst.lastModified() || src.length() != dst.length()) {
- FileUtils.copyFile(src, dst);
- dst.setLastModified(src.lastModified());
- }
- }
+ CollectRequest collectRequest = new CollectRequest();
+ collectRequest.setRootArtifact(RepositoryUtils.toArtifact(project.getArtifact()));
+ collectRequest.setDependencies(dependencies);
+ collectRequest.setManagedDependencies(managedDependencies);
- /**
- * Installs the main artifact and any attached artifacts of the specified project to the local repository.
- *
- * @param mvnProject The project whose artifacts should be installed, must not be null
.
- * @throws MojoExecutionException If any artifact could not be installed.
- */
- private void installProjectArtifacts(MavenProject mvnProject) throws MojoExecutionException {
- try {
- // Install POM (usually attached as metadata but that happens only as a side effect of the Install Plugin)
- installProjectPom(mvnProject);
+ collectRequest.setRepositories(project.getRemoteProjectRepositories());
- // Install the main project artifact (if the project has one, e.g. has no "pom" packaging)
- Artifact mainArtifact = mvnProject.getArtifact();
- if (mainArtifact.getFile() != null) {
- installArtifact(mainArtifact.getFile(), mainArtifact);
- }
+ DependencyRequest request = new DependencyRequest(collectRequest, classpathFilter);
- // Install any attached project artifacts
- Collection attachedArtifacts = mvnProject.getAttachedArtifacts();
- for (Artifact attachedArtifact : attachedArtifacts) {
- installArtifact(attachedArtifact.getFile(), attachedArtifact);
- }
- } catch (Exception e) {
- throw new MojoExecutionException("Failed to install project artifacts: " + mvnProject, e);
- }
- }
+ DependencyResult dependencyResult =
+ repositorySystem.resolveDependencies(session.getRepositorySession(), request);
- /**
- * Installs the (locally reachable) parent POMs of the specified project to the local repository. The parent POMs
- * from the reactor must be installed or the forked IT builds will fail when using a clean repository.
- *
- * @param mvnProject The project whose parent POMs should be installed, must not be null
.
- * @throws MojoExecutionException If any POM could not be installed.
- */
- private void installProjectParents(MavenProject mvnProject) throws MojoExecutionException {
- try {
- for (MavenProject parent = mvnProject.getParent(); parent != null; parent = parent.getParent()) {
- if (parent.getFile() == null) {
- copyParentPoms(parent.getGroupId(), parent.getArtifactId(), parent.getVersion());
- break;
- }
- installProjectPom(parent);
- }
- } catch (Exception e) {
- throw new MojoExecutionException("Failed to install project parents: " + mvnProject, e);
- }
- }
+ List artifacts = dependencyResult.getArtifactResults().stream()
+ .map(ArtifactResult::getArtifact)
+ .collect(Collectors.toList());
- /**
- * Installs the POM of the specified project to the local repository.
- *
- * @param mvnProject The project whose POM should be installed, must not be null
.
- * @throws MojoExecutionException If the POM could not be installed.
- */
- private void installProjectPom(MavenProject mvnProject) throws MojoExecutionException {
- try {
- Artifact pomArtifact = null;
- if ("pom".equals(mvnProject.getPackaging())) {
- pomArtifact = mvnProject.getArtifact();
- }
- if (pomArtifact == null) {
- pomArtifact = artifactFactory.createProjectArtifact(
- mvnProject.getGroupId(), mvnProject.getArtifactId(), mvnProject.getVersion());
- }
- installArtifact(mvnProject.getFile(), pomArtifact);
- } catch (Exception e) {
- throw new MojoExecutionException("Failed to install POM: " + mvnProject, e);
- }
+ resolvedArtifacts.addAll(artifacts);
+ resolvePomsForArtifacts(artifacts, resolvedArtifacts, collectRequest.getRepositories());
}
/**
- * Installs the dependent projects from the reactor to the local repository. The dependencies on other modules from
- * the reactor must be installed or the forked IT builds will fail when using a clean repository.
+ * Resolve extra artifacts.
*
- * @param mvnProject The project whose dependent projects should be installed, must not be null
.
- * @param reactorProjects The set of projects in the reactor build, must not be null
.
- * @throws MojoExecutionException If any dependency could not be installed.
+ * @return
*/
- private void installProjectDependencies(MavenProject mvnProject, Collection reactorProjects)
- throws MojoExecutionException {
- // ... into dependencies that were resolved from reactor projects ...
- Collection dependencyProjects = new LinkedHashSet<>();
- collectAllProjectReferences(mvnProject, dependencyProjects);
-
- // index available reactor projects
- Map projects = new HashMap<>(reactorProjects.size());
- for (MavenProject reactorProject : reactorProjects) {
- String projectId = reactorProject.getGroupId()
- + ':'
- + reactorProject.getArtifactId()
- + ':'
- + reactorProject.getVersion();
-
- projects.put(projectId, reactorProject);
- }
-
- // group transitive dependencies (even those that don't contribute to the class path like POMs) ...
- Collection artifacts = mvnProject.getArtifacts();
- // ... and those that were resolved from the (local) repo
- Collection dependencyArtifacts = new LinkedHashSet<>();
+ private void resolveExtraArtifacts(Collection resolvedArtifacts)
+ throws MojoExecutionException, DependencyResolutionException, ArtifactDescriptorException,
+ ArtifactResolutionException {
- for (Artifact artifact : artifacts) {
- // workaround for MNG-2961 to ensure the base version does not contain a timestamp
- artifact.isSnapshot();
+ if (extraArtifacts == null) {
+ return;
+ }
- String projectId = artifact.getGroupId() + ':' + artifact.getArtifactId() + ':' + artifact.getBaseVersion();
+ DependencyFilter classpathFilter = DependencyFilterUtils.classpathFilter(JavaScopes.RUNTIME);
- if (!projects.containsKey(projectId)) {
- dependencyArtifacts.add(artifact);
+ for (String extraArtifact : extraArtifacts) {
+ String[] gav = extraArtifact.split(":");
+ if (gav.length < 3 || gav.length > 5) {
+ throw new MojoExecutionException("Invalid artifact " + extraArtifact);
}
- }
- // install dependencies
- try {
- // copy dependencies that where resolved from the local repo
- for (Artifact artifact : dependencyArtifacts) {
- copyArtifact(artifact);
- }
+ String groupId = gav[0];
+ String artifactId = gav[1];
+ String version = gav[2];
- // install dependencies that were resolved from the reactor
- for (String projectId : dependencyProjects) {
- MavenProject dependencyProject = projects.get(projectId);
- if (dependencyProject == null) {
- getLog().warn("skip dependencyProject null for projectId=" + projectId);
- continue;
- }
- installProjectArtifacts(dependencyProject);
- installProjectParents(dependencyProject);
+ String type = "jar";
+ if (gav.length > 3) {
+ type = gav[3];
}
- } catch (Exception e) {
- throw new MojoExecutionException("Failed to install project dependencies: " + mvnProject, e);
- }
- }
- protected void collectAllProjectReferences(MavenProject project, Collection dependencyProjects) {
- for (MavenProject reactorProject : project.getProjectReferences().values()) {
- String projectId = reactorProject.getGroupId()
- + ':'
- + reactorProject.getArtifactId()
- + ':'
- + reactorProject.getVersion();
- if (dependencyProjects.add(projectId)) {
- collectAllProjectReferences(reactorProject, dependencyProjects);
+ String classifier = null;
+ if (gav.length == 5) {
+ classifier = gav[4];
}
- }
- }
- private void copyArtifact(Artifact artifact) throws MojoExecutionException {
- copyPoms(artifact);
+ ArtifactType artifactType =
+ session.getRepositorySession().getArtifactTypeRegistry().get(type);
- Artifact depArtifact = artifactFactory.createArtifactWithClassifier(
- artifact.getGroupId(),
- artifact.getArtifactId(),
- artifact.getBaseVersion(),
- artifact.getType(),
- artifact.getClassifier());
+ List remoteRepositories =
+ artifactType != null && "maven-plugin".equals(artifactType.getId())
+ ? project.getRemotePluginRepositories()
+ : project.getRemoteProjectRepositories();
- File artifactFile = artifact.getFile();
+ Artifact artifact = new DefaultArtifact(groupId, artifactId, classifier, null, version, artifactType);
- copyArtifact(artifactFile, depArtifact);
- }
+ resolvePomsForArtifacts(Collections.singletonList(artifact), resolvedArtifacts, remoteRepositories);
+
+ CollectRequest collectRequest = new CollectRequest();
+ Dependency root = new Dependency(artifact, JavaScopes.COMPILE);
+ collectRequest.setRoot(root);
+ collectRequest.setRepositories(remoteRepositories);
- private void copyPoms(Artifact artifact) throws MojoExecutionException {
- Artifact pomArtifact = artifactFactory.createProjectArtifact(
- artifact.getGroupId(), artifact.getArtifactId(), artifact.getBaseVersion());
+ DependencyRequest request = new DependencyRequest(collectRequest, classpathFilter);
+ DependencyResult dependencyResult =
+ repositorySystem.resolveDependencies(session.getRepositorySession(), request);
- File pomFile = new File(localRepository.getBasedir(), localRepository.pathOf(pomArtifact));
+ List artifacts = dependencyResult.getArtifactResults().stream()
+ .map(ArtifactResult::getArtifact)
+ .collect(Collectors.toList());
- if (pomFile.isFile()) {
- copyArtifact(pomFile, pomArtifact);
- copyParentPoms(pomFile);
+ resolvedArtifacts.addAll(artifacts);
+ resolvePomsForArtifacts(artifacts, resolvedArtifacts, collectRequest.getRepositories());
}
}
- /**
- * Installs all parent POMs of the specified POM file that are available in the local repository.
- *
- * @param pomFile The path to the POM file whose parents should be installed, must not be null
.
- * @throws MojoExecutionException If any (existing) parent POM could not be installed.
- */
- private void copyParentPoms(File pomFile) throws MojoExecutionException {
- Model model = PomUtils.loadPom(pomFile);
- Parent parent = model.getParent();
- if (parent != null) {
- copyParentPoms(parent.getGroupId(), parent.getArtifactId(), parent.getVersion());
+ private void resolvePomsForArtifacts(
+ List artifacts, Collection resolvedArtifacts, List remoteRepositories)
+ throws ArtifactResolutionException, MojoExecutionException {
+
+ for (Artifact a : artifacts) {
+ Artifact artifactResult = resolveArtifact(new SubArtifact(a, "", "pom"), remoteRepositories);
+ resolvePomWithParents(artifactResult, resolvedArtifacts, remoteRepositories);
}
}
- /**
- * Installs the specified POM and all its parent POMs to the local repository.
- *
- * @param groupId The group id of the POM which should be installed, must not be null
.
- * @param artifactId The artifact id of the POM which should be installed, must not be null
.
- * @param version The version of the POM which should be installed, must not be null
.
- * @throws MojoExecutionException If any (existing) parent POM could not be installed.
- */
- private void copyParentPoms(String groupId, String artifactId, String version) throws MojoExecutionException {
- Artifact pomArtifact = artifactFactory.createProjectArtifact(groupId, artifactId, version);
+ private void resolvePomWithParents(
+ Artifact artifact, Collection resolvedArtifacts, List remoteRepositories)
+ throws MojoExecutionException, ArtifactResolutionException {
- if (installedArtifacts.contains(pomArtifact.getId()) || copiedArtifacts.contains(pomArtifact.getId())) {
- getLog().debug("Not re-installing " + pomArtifact);
+ if (resolvedArtifacts.contains(artifact)) {
return;
}
- File pomFile = new File(localRepository.getBasedir(), localRepository.pathOf(pomArtifact));
- if (pomFile.isFile()) {
- copyArtifact(pomFile, pomArtifact);
- copyParentPoms(pomFile);
+ Model model = PomUtils.loadPom(artifact.getFile());
+ Parent parent = model.getParent();
+ if (parent != null) {
+ DefaultArtifact pom =
+ new DefaultArtifact(parent.getGroupId(), parent.getArtifactId(), "", "pom", parent.getVersion());
+ Artifact resolvedPom = resolveArtifact(pom, remoteRepositories);
+ resolvePomWithParents(resolvedPom, resolvedArtifacts, remoteRepositories);
}
+
+ resolvedArtifacts.add(artifact);
}
- private void installExtraArtifacts(String[] extraArtifacts) throws MojoExecutionException {
- if (extraArtifacts == null) {
- return;
- }
+ private Artifact resolveArtifact(Artifact artifact, List remoteRepositories)
+ throws ArtifactResolutionException {
- for (String extraArtifact : extraArtifacts) {
- String[] gav = extraArtifact.split(":");
- if (gav.length < 3 || gav.length > 5) {
- throw new MojoExecutionException("Invalid artifact " + extraArtifact);
- }
+ ArtifactRequest request = new ArtifactRequest();
+ request.setArtifact(artifact);
+ request.setRepositories(remoteRepositories);
+ ArtifactResult artifactResult = repositorySystem.resolveArtifact(session.getRepositorySession(), request);
+ return artifactResult.getArtifact();
+ }
- String groupId = gav[0];
- String artifactId = gav[1];
- String version = gav[2];
+ /**
+ * Install list of artifacts into local repository.
+ */
+ private void installArtifacts(Collection resolvedArtifacts) throws InstallationException {
- String type = "jar";
- if (gav.length > 3) {
- type = gav[3];
- }
+ // we can have on dependency two artifacts with the same groupId:artifactId
+ // with different version, in such case when we install both in one request
+ // metadata will contain only one version
- String classifier = null;
- if (gav.length == 5) {
- classifier = gav[4];
- }
+ Map> collect = resolvedArtifacts.stream()
+ .collect(Collectors.groupingBy(
+ a -> String.format("%s:%s:%s", a.getGroupId(), a.getArtifactId(), a.getVersion()),
+ LinkedHashMap::new,
+ Collectors.toList()));
+
+ RepositorySystemSession systemSessionForLocalRepo = createSystemSessionForLocalRepo();
+
+ for (List artifacts : collect.values()) {
+ InstallRequest request = new InstallRequest();
+ request.setArtifacts(artifacts);
+ repositorySystem.install(systemSessionForLocalRepo, request);
+ }
+ }
- DefaultDependableCoordinate coordinate = new DefaultDependableCoordinate();
- try {
- coordinate.setGroupId(groupId);
- coordinate.setArtifactId(artifactId);
- coordinate.setVersion(version);
- coordinate.setType(type);
- coordinate.setClassifier(classifier);
-
- if (!localRepository.getBasedir().equals(localRepositoryPath.getPath()) && useLocalRepository) {
- String previousId = localRepository.getId();
- try {
- // using another request with the correct target repo
- ProjectBuildingRequest projectBuildingRequest = repositoryManager.setLocalRepositoryBasedir(
- session.getProjectBuildingRequest(), localRepositoryPath);
- projectBuildingRequest.setRemoteRepositories(Collections.singletonList(localRepository));
- resolver.resolveDependencies(
- projectBuildingRequest,
- coordinate,
- new PatternExclusionsFilter(Collections.emptyList()));
- } finally {
- localRepository.setId(previousId);
- }
- } else {
- resolver.resolveDependencies(
- projectBuildingRequest, coordinate, new PatternExclusionsFilter(Collections.emptyList()));
- }
- } catch (DependencyResolverException e) {
- throw new MojoExecutionException("Unable to resolve dependencies for: " + coordinate, e);
+ /**
+ * Create a new {@link RepositorySystemSession} connected with local repo.
+ */
+ private RepositorySystemSession createSystemSessionForLocalRepo() {
+ RepositorySystemSession repositorySystemSession = session.getRepositorySession();
+ 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());
}
+
+ return repositorySystemSession;
}
}
diff --git a/src/main/java/org/apache/maven/plugins/invoker/MetadataUtils.java b/src/main/java/org/apache/maven/plugins/invoker/MetadataUtils.java
deleted file mode 100644
index 13d2c4a4..00000000
--- a/src/main/java/org/apache/maven/plugins/invoker/MetadataUtils.java
+++ /dev/null
@@ -1,146 +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.invoker;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.Reader;
-import java.io.Writer;
-import java.text.SimpleDateFormat;
-import java.util.Collection;
-import java.util.Date;
-import java.util.LinkedHashSet;
-import java.util.Set;
-import java.util.TimeZone;
-
-import org.apache.maven.artifact.Artifact;
-import org.codehaus.plexus.util.ReaderFactory;
-import org.codehaus.plexus.util.WriterFactory;
-import org.codehaus.plexus.util.xml.Xpp3Dom;
-import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
-import org.codehaus.plexus.util.xml.Xpp3DomUtils;
-import org.codehaus.plexus.util.xml.Xpp3DomWriter;
-import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
-
-/**
- * Provides utility methods for artifact metadata processing.
- *
- * @author Benjamin Bentmann
- */
-class MetadataUtils {
-
- /**
- * Creates local metadata files for the specified artifact. The goal is to simulate the installation of the artifact
- * by a local build, thereby decoupling the forked builds from the inderministic collection of remote repositories
- * that are available to the main build and from which the artifact was originally resolved.
- *
- * @param file The artifact's file in the local test repository, must not be null
.
- * @param artifact The artifact to create metadata for, must not be null
.
- * @throws IOException If the metadata could not be created.
- */
- public static void createMetadata(File file, Artifact artifact) throws IOException {
- TimeZone tz = java.util.TimeZone.getTimeZone("UTC");
- SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmss");
- fmt.setTimeZone(tz);
- String timestamp = fmt.format(new Date());
-
- if (artifact.isSnapshot()) {
- File metadataFile = new File(file.getParentFile(), "maven-metadata-local.xml");
-
- Xpp3Dom metadata = new Xpp3Dom("metadata");
- addChild(metadata, "groupId", artifact.getGroupId());
- addChild(metadata, "artifactId", artifact.getArtifactId());
- addChild(metadata, "version", artifact.getBaseVersion());
- Xpp3Dom versioning = new Xpp3Dom("versioning");
- versioning.addChild(addChild(new Xpp3Dom("snapshot"), "localCopy", "true"));
- addChild(versioning, "lastUpdated", timestamp);
- metadata.addChild(versioning);
-
- writeMetadata(metadataFile, metadata);
- }
-
- File metadataFile = new File(file.getParentFile().getParentFile(), "maven-metadata-local.xml");
-
- Set allVersions = new LinkedHashSet<>();
-
- Xpp3Dom metadata = readMetadata(metadataFile);
-
- if (metadata != null) {
- Xpp3Dom versioning = metadata.getChild("versioning");
- if (versioning != null) {
- Xpp3Dom versions = versioning.getChild("versions");
- if (versions != null) {
-
- Xpp3Dom[] children = versions.getChildren("version");
- for (Xpp3Dom aChildren : children) {
- allVersions.add(aChildren.getValue());
- }
- }
- }
- }
-
- allVersions.add(artifact.getBaseVersion());
-
- metadata = new Xpp3Dom("metadata");
- addChild(metadata, "groupId", artifact.getGroupId());
- addChild(metadata, "artifactId", artifact.getArtifactId());
- Xpp3Dom versioning = new Xpp3Dom("versioning");
- versioning.addChild(addChildren(new Xpp3Dom("versions"), "version", allVersions));
- addChild(versioning, "lastUpdated", timestamp);
- metadata.addChild(versioning);
-
- metadata = Xpp3DomUtils.mergeXpp3Dom(metadata, readMetadata(metadataFile));
-
- writeMetadata(metadataFile, metadata);
- }
-
- private static Xpp3Dom addChild(Xpp3Dom parent, String childName, String childValue) {
- Xpp3Dom child = new Xpp3Dom(childName);
- child.setValue(childValue);
- parent.addChild(child);
- return parent;
- }
-
- private static Xpp3Dom addChildren(Xpp3Dom parent, String childName, Collection childValues) {
- for (String childValue : childValues) {
- addChild(parent, childName, childValue);
- }
- return parent;
- }
-
- private static Xpp3Dom readMetadata(File metadataFile) throws IOException {
- if (!metadataFile.isFile()) {
- return null;
- }
-
- try (Reader reader = ReaderFactory.newXmlReader(metadataFile)) {
- return Xpp3DomBuilder.build(reader);
- } catch (XmlPullParserException e) {
- throw new IOException(e.getMessage(), e);
- }
- }
-
- private static void writeMetadata(File metadataFile, Xpp3Dom metadata) throws IOException {
- metadataFile.getParentFile().mkdirs();
-
- try (Writer writer = WriterFactory.newXmlWriter(metadataFile)) {
- Xpp3DomWriter.write(writer, metadata);
- }
- }
-}