Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* OW2 Util
* Copyright (C) 2009 Bull S.A.S.
* Contact: [email protected]
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
* --------------------------------------------------------------------------
* $Id: VersionsMojo.java 4955 2009-05-13 12:24:17Z fornacif $
* --------------------------------------------------------------------------
*/
package org.ow2.util.maven.jbuilding;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.Stack;
import java.util.TreeMap;
import java.util.regex.Pattern;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
import org.apache.maven.artifact.resolver.ArtifactCollector;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
import org.apache.maven.artifact.resolver.ResolutionNode;
import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.codehaus.plexus.util.FileUtils;
/**
* Process a project and explore artifacts versions.
* @goal versions
* @phase process-sources
* @author Guillaume Sauthier
*/
public class VersionsMojo extends AbstractResolverMojo {
/**
* Property pattern.
*/
private static Pattern PROPERTY_NAME_PATTERN = Pattern.compile(".*\\.version$");
/**
* Location of the versions.properties to be created.
* @parameter expression="${project.build.directory}/versions.properties"
* @required
*/
private File output;
/**
* Group Ids to be joined if they share the same version.
* @parameter
*/
private List joinedGroupIds = new ArrayList();
/**
* @parameter expression="${jbuilding.versions.properties}" default-value="false"
*/
private boolean browseProperties = false;
/**
* @parameter
*/
private String[] preferedGroupIds;
/**
* Dependencies depth filtering.
* @parameter
*/
private int depth = 3;
/**
* ArtifactCollector filtering on artifact's scope.
* @parameter
*/
private String collectScope = Artifact.SCOPE_COMPILE;
/**
* The collector artifact used to discover dependencies childrens.
* @component
* @required
* @readonly
*/
private ArtifactCollector collector;
/**
* @component
* @required
* @readonly
*/
private ArtifactMetadataSource artifactMetadataSource;
/**
* Joined groupIds with their default version.
*/
private Map joinGroups = new HashMap();
/**
* Llist of resolvables artifacts (to be used with the collector to resolve first depth dependencies).
*/
private Set collectableArtifacts = new HashSet();
/**
* Default Bean constructor.
*/
public VersionsMojo() {
// Default value : no preferences
preferedGroupIds = new String[1];
preferedGroupIds[0] = "*";
}
/**
* Writes the versions.properties file.
* @throws MojoExecutionException
* @throws MojoFailureException
*/
public void execute() throws MojoExecutionException, MojoFailureException {
getLog().debug("Introspecting project " + getProject().getGroupId() + ":" + getProject().getArtifactId());
// Resolve the artifacts provided in the plugin configuration
Map> map = resolveArtifacts(SERVER_SIDE, BOTH_MODE, ALL_ARTIFACT_ITEMS);
// Init the Version map
Map versionsProperties = new TreeMap();
// Browse properties
if (browseProperties) {
getVersionsFromModelProperties(versionsProperties);
getVersionsFromProjectProperties(versionsProperties);
}
// Browse dependencies
getVersionsFromArtifactItems(map, versionsProperties);
getVersionsFromProjectDependencies(versionsProperties);
// Browse transitive dependencies
getVersionsFromTransitiveDependencies(versionsProperties);
// Don't forget joined group Ids
getVersionsFromJoinedGroupIds(versionsProperties);
// Store the file
try {
getLog().debug("Storing in " + output);
storeVersionsProperties(versionsProperties);
} catch (IOException ioe) {
throw new MojoExecutionException("Cannot store " + output, ioe);
}
}
/**
* Extract version information from project's runtime properties
* @param versions versions
*/
public void getVersionsFromProjectProperties(final Map versions) {
getLog().debug("Looking in project's properties");
// Look on the project runtime properties
filterProperties(getProject().getProperties(), versions);
}
/**
* Extract version information from project's model properties
* @param versions versions
*/
public void getVersionsFromModelProperties(final Map versions) {
getLog().debug("Looking in project's model properties");
// Look on the ${project.properties.*} values
filterProperties(getProject().getModel().getProperties(), versions);
}
/**
* Extract versions informations from ArtifactItems listed in the jbuilding plugin configuration.
* @param groups groups of ArtifactItems
* @param versions versions
*/
public void getVersionsFromArtifactItems(final Map> groups, final Map versions) {
// Iterates over groups (~ deployment-plans)
for (Map.Entry> group : groups.entrySet()) {
// Iterates over artifact items in the group
for (Artifact artifact : group.getValue()) {
String groupId = artifact.getGroupId();
String artifactVersion = artifact.getVersion();
if (!filterGroupId(groupId, artifactVersion)) {
// if the artifact did need to be filtered, just write the property
String name = getArtifactName(artifact.getGroupId(),
artifact.getArtifactId());
addVersion(versions, name, artifactVersion);
}
// Add this artifact
collectableArtifacts.add(artifact);
}
}
}
/**
* Extract versions informations from project's dependencies.
* @param versions versions
*/
@SuppressWarnings({"unchecked"})
public void getVersionsFromProjectDependencies(final Map versions) {
// Iterates on all project's dependencies
List dependencies = getProject().getDependencies();
if (dependencies != null) {
for (Dependency dependency : dependencies) {
String groupId = dependency.getGroupId();
String artifactId = dependency.getArtifactId();
String dependencyVersion = dependency.getVersion();
String dependencyType = dependency.getType();
String dependencyClassifier = dependency.getClassifier();
if (!filterGroupId(groupId, dependencyVersion)) {
// if the artifact didn't need to be filtered, just write the property
String name = getArtifactName(groupId,
artifactId);
addVersion(versions, name, dependencyVersion);
}
// Add this artifact
collectableArtifacts.add(createArtifact(groupId, artifactId, dependencyVersion, dependencyType, dependencyClassifier));
}
}
}
/**
* Extract versions information from transitive dependencies of the
* collected artifacts (dependencies + bundle items)
* @param versions versions
* @throws MojoExecutionException if POM collection failed
*/
private void getVersionsFromTransitiveDependencies(final Map versions) throws MojoExecutionException {
try {
// Collect metadata informations from all the collected artifacts
// This doesn't download the artifacts, only the POM.
ArtifactResolutionResult result = collector.collect(collectableArtifacts,
getProject().getArtifact(),
getLocal(),
getRemoteRepositories(),
artifactMetadataSource,
new ScopeArtifactFilter(collectScope),
Collections.emptyList());
Set nodes = result.getArtifactResolutionNodes();
for (ResolutionNode node : nodes) {
// Only manage firsts depths nodes
if (node.getDepth() < depth) {
Artifact artifact = node.getArtifact();
String groupId = artifact.getGroupId();
String artifactId = artifact.getArtifactId();
String dependencyVersion = artifact.getVersion();
if (!filterGroupId(groupId, dependencyVersion)) {
// if the artifact didn't need to be filtered, just write the property
String name = getArtifactName(groupId,
artifactId);
addVersion(versions, name, dependencyVersion);
}
}
}
} catch (ArtifactResolutionException e) {
throw new MojoExecutionException("Cannot resolve artifacts", e);
}
}
/**
* Extract versions informations from joined groupIds.
* @param versions versions
*/
public void getVersionsFromJoinedGroupIds(final Map versions) {
for (Map.Entry entry : joinGroups.entrySet()) {
String name = entry.getKey();
String version = entry.getValue();
addVersion(versions, name, version);
}
}
/**
* Filter properties values and only select the one finishing with ".version"
* @param filteredProperties properties to be filtered
* @param versions versions
*/
private void filterProperties(final Properties filteredProperties,
final Map versions) {
if (filteredProperties != null) {
// Filter all existing properties
for (Map.Entry