Java Code Examples for org.apache.maven.artifact.ArtifactUtils#versionlessKey()

The following examples show how to use org.apache.maven.artifact.ArtifactUtils#versionlessKey() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MavenHelper.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Build the map of dependencies for the current plugin.
 *
 * @return the artifact.
 * @throws MojoExecutionException if the current plugin cannot be determined.
 */
public synchronized Map<String, Dependency> getPluginDependencies() throws MojoExecutionException {
	if (this.pluginDependencies == null) {
		final String groupId = getConfig("plugin.groupId"); //$NON-NLS-1$
		final String artifactId = getConfig("plugin.artifactId"); //$NON-NLS-1$
		final String pluginArtifactKey = ArtifactUtils.versionlessKey(groupId, artifactId);

		final Set<Artifact> dependencies = resolveDependencies(pluginArtifactKey, true);

		final Map<String, Dependency> deps = new TreeMap<>();

		for (final Artifact artifact : dependencies) {
			final Dependency dep = toDependency(artifact);
			deps.put(ArtifactUtils.versionlessKey(artifact), dep);
		}

		this.pluginDependencies = deps;
	}
	return this.pluginDependencies;
}
 
Example 2
Source File: MavenHelper.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Replies the version of the given plugin that is specified in the POM of the
 * plugin in which this mojo is located.
 *
 * @param groupId the identifier of the group.
 * @param artifactId thidentifier of the artifact.
 * @return the version, never {@code null}
 * @throws MojoExecutionException if the plugin was not found.
 */
public String getPluginDependencyVersion(String groupId, String artifactId) throws MojoExecutionException {
	final Map<String, Dependency> deps = getPluginDependencies();
	final String key = ArtifactUtils.versionlessKey(groupId, artifactId);
	this.log.debug("COMPONENT DEPENDENCIES(getPluginVersionFromDependencies):"); //$NON-NLS-1$
	this.log.debug(deps.toString());
	final Dependency dep = deps.get(key);
	if (dep != null) {
		final String version = dep.getVersion();
		if (version != null && !version.isEmpty()) {
			return version;
		}
		throw new MojoExecutionException(MessageFormat.format(Messages.MavenHelper_2, key));
	}
	throw new MojoExecutionException(MessageFormat.format(Messages.MavenHelper_3, key, deps));
}
 
Example 3
Source File: AbstractSarlMojo.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Extract the dependencies that are declared for a Maven plugin.
 * This function reads the list of the dependencies in the configuration
 * resource file with {@link MavenHelper#getConfig(String)}.
 * The key given to {@link MavenHelper#getConfig(String)} is
 * <code>&lt;configurationKeyPrefix&gt;.dependencies</code>.
 *
 * @param configurationKeyPrefix the string that is the prefix in the configuration file.
 * @return the list of the dependencies.
 * @throws MojoExecutionException if something cannot be done when extracting the dependencies.
 */
protected Dependency[] getDependenciesFor(String configurationKeyPrefix) throws MojoExecutionException {
	final List<Dependency> dependencies = new ArrayList<>();
	final Pattern pattern = Pattern.compile(
			"^[ \t\n\r]*([^: \t\n\t]+)[ \t\n\r]*:[ \t\n\r]*([^: \t\n\t]+)[ \t\n\r]*$"); //$NON-NLS-1$
	final String rawDependencies = this.mavenHelper.getConfig(configurationKeyPrefix + ".dependencies"); //$NON-NLS-1$

	final Map<String, Dependency> pomDependencies = this.mavenHelper.getPluginDependencies();

	for (final String dependencyId : rawDependencies.split("\\s*[;|,]+\\s*")) { //$NON-NLS-1$
		final Matcher matcher = pattern.matcher(dependencyId);
		if (matcher != null && matcher.matches()) {
			final String dependencyGroupId = matcher.group(1);
			final String dependencyArtifactId = matcher.group(2);
			final String dependencyKey = ArtifactUtils.versionlessKey(dependencyGroupId, dependencyArtifactId);
			final Dependency dependencyObject = pomDependencies.get(dependencyKey);
			if (dependencyObject == null) {
				throw new MojoExecutionException(MessageFormat.format(
						Messages.AbstractSarlMojo_4, dependencyKey));
			}
			dependencies.add(dependencyObject);
		}
	}

	final Dependency[] dependencyArray = new Dependency[dependencies.size()];
	dependencies.toArray(dependencyArray);
	return dependencyArray;
}
 
Example 4
Source File: DefaultArtifactAssociation.java    From versions-maven-plugin with Apache License 2.0 4 votes vote down vote up
public String toString()
{
    return ( usePluginRepositories ? "plugin:" : "artifact:" ) + ArtifactUtils.versionlessKey( artifact );
}
 
Example 5
Source File: DisplayDependencyUpdatesMojo.java    From versions-maven-plugin with Apache License 2.0 4 votes vote down vote up
private void logUpdates( Map<Dependency, ArtifactVersions> updates, String section )
{
    List<String> withUpdates = new ArrayList<>();
    List<String> usingCurrent = new ArrayList<>();
    Iterator i = updates.values().iterator();
    while ( i.hasNext() )
    {
        ArtifactVersions versions = (ArtifactVersions) i.next();
        String left = "  " + ArtifactUtils.versionlessKey( versions.getArtifact() ) + " ";
        final String current;
        ArtifactVersion latest;
        if ( versions.isCurrentVersionDefined() )
        {
            current = versions.getCurrentVersion().toString();
            latest = versions.getNewestUpdate( calculateUpdateScope(), allowSnapshots );
        }
        else
        {
            ArtifactVersion newestVersion =
                versions.getNewestVersion( versions.getArtifact().getVersionRange(), allowSnapshots );
            current = versions.getArtifact().getVersionRange().toString();
            latest = newestVersion == null ? null
                            : versions.getNewestUpdate( newestVersion, calculateUpdateScope(), allowSnapshots );
            if ( latest != null
                && ArtifactVersions.isVersionInRange( latest, versions.getArtifact().getVersionRange() ) )
            {
                latest = null;
            }
        }
        String right = " " + ( latest == null ? current : current + " -> " + latest.toString() );
        List<String> t = latest == null ? usingCurrent : withUpdates;
        if ( right.length() + left.length() + 3 > INFO_PAD_SIZE )
        {
            t.add( left + "..." );
            t.add( StringUtils.leftPad( right, INFO_PAD_SIZE ) );

        }
        else
        {
            t.add( StringUtils.rightPad( left, INFO_PAD_SIZE - right.length(), "." ) + right );
        }
    }

    if ( isVerbose() )
    {
        if ( usingCurrent.isEmpty() )
        {
            if ( !withUpdates.isEmpty() )
            {
                logLine( false, "No dependencies in " + section + " are using the newest version." );
                logLine( false, "" );
            }
        }
        else
        {
            logLine( false, "The following dependencies in " + section + " are using the newest version:" );
            i = usingCurrent.iterator();
            while ( i.hasNext() )
            {
                logLine( false, (String) i.next() );
            }
            logLine( false, "" );
        }
    }        
    
    
    if ( withUpdates.isEmpty() )
    {
        if ( !usingCurrent.isEmpty() )
        {
            logLine( false, "No dependencies in " + section + " have newer versions." );
            logLine( false, "" );
        }
    }
    else
    {
        logLine( false, "The following dependencies in " + section + " have newer versions:" );
        i = withUpdates.iterator();
        while ( i.hasNext() )
        {
            logLine( false, (String) i.next() );
        }
        logLine( false, "" );
    }
}
 
Example 6
Source File: AbstractCompileMojo.java    From sarl with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private void validateDependencyVersions() throws MojoExecutionException, MojoFailureException {
	getLog().info(Messages.CompileMojo_7);
	final String sarlSdkGroupId = this.mavenHelper.getConfig("sarl-sdk.groupId"); //$NON-NLS-1$
	final String sarlSdkArtifactId = this.mavenHelper.getConfig("sarl-sdk.artifactId"); //$NON-NLS-1$

	boolean hasError = false;

	final Map<String, Artifact> projectDependencyTree = this.mavenHelper.getSession().getCurrentProject().getArtifactMap();
	final String sdkArtifactKey = ArtifactUtils.versionlessKey(sarlSdkGroupId, sarlSdkArtifactId);
	final Artifact sdkArtifact = projectDependencyTree.get(sdkArtifactKey);
	if (sdkArtifact != null) {
		final Map<String, ArtifactVersion> pluginDependencyTree = new TreeMap<>();
		final Set<Artifact> pluginScopeDependencies = this.mavenHelper.resolveDependencies(sdkArtifactKey, false);
		for (final Artifact pluginScopeDependency : pluginScopeDependencies) {
			final ArtifactVersion pluginScopeDependencyVersion = new DefaultArtifactVersion(pluginScopeDependency.getVersion());
			final String pluginScopeDependencyKey = ArtifactUtils.versionlessKey(pluginScopeDependency);
			final ArtifactVersion currentVersion = pluginDependencyTree.get(pluginScopeDependencyKey);
			if (currentVersion == null || pluginScopeDependencyVersion.compareTo(currentVersion) > 0) {
				pluginDependencyTree.put(pluginScopeDependencyKey, pluginScopeDependencyVersion);
			}
		}

		for (final Entry<String, Artifact> projectDependency : projectDependencyTree.entrySet()) {
			final ArtifactVersion pluginDependencyArtifactVersion = pluginDependencyTree.get(projectDependency.getKey());
			if (pluginDependencyArtifactVersion != null) {
				final Artifact projectArtifact = projectDependency.getValue();
				final ArtifactVersion projectDependencyVersion = new DefaultArtifactVersion(projectArtifact.getVersion());
				if (Utils.compareMajorMinorVersions(pluginDependencyArtifactVersion, projectDependencyVersion) != 0) {
					final String message = MessageFormat.format(Messages.CompileMojo_8,
							projectArtifact.getGroupId(), projectArtifact.getArtifactId(),
							pluginDependencyArtifactVersion.toString(), projectDependencyVersion.toString());
					getLog().error(message);
					hasError = true;
				}
			}
		}
	}

	if (hasError) {
		throw new MojoFailureException(Messages.CompileMojo_10);
	}
}