Example usage for org.apache.maven.artifact ArtifactUtils versionlessKey

List of usage examples for org.apache.maven.artifact ArtifactUtils versionlessKey

Introduction

In this page you can find the example usage for org.apache.maven.artifact ArtifactUtils versionlessKey.

Prototype

public static String versionlessKey(String groupId, String artifactId) 

Source Link

Usage

From source file:hudson.maven.ReactorReader.java

License:Apache License

public ReactorReader(Map<String, MavenProject> reactorProjects, File workspaceRoot) {
    projectsByGAV = reactorProjects;/*from  w  w  w  .j  a  v  a 2 s  . co  m*/
    this.workspaceRoot = workspaceRoot;
    projectsByGA = new HashMap<String, List<MavenProject>>(reactorProjects.size() * 2);
    for (MavenProject project : reactorProjects.values()) {
        String key = ArtifactUtils.versionlessKey(project.getGroupId(), project.getArtifactId());

        List<MavenProject> projects = projectsByGA.get(key);

        if (projects == null) {
            projects = new ArrayList<MavenProject>(1);
            projectsByGA.put(key, projects);
        }

        projects.add(project);
    }

    repository = new WorkspaceRepository("reactor", new HashSet<String>(projectsByGAV.keySet()));
}

From source file:io.sarl.maven.compiler.AbstractSarlMojo.java

License:Apache License

/** 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.
 */// ww w .j  av a2 s . c  o  m
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(
                        Locale.getString(AbstractSarlMojo.class, "ARTIFACT_NOT_FOUND", dependencyKey)); //$NON-NLS-1$
            }
            dependencies.add(dependencyObject);
        }
    }

    final Dependency[] dependencyArray = new Dependency[dependencies.size()];
    dependencies.toArray(dependencyArray);
    return dependencyArray;
}

From source file:io.sarl.maven.compiler.CompileMojo.java

License:Apache License

@SuppressWarnings("unchecked")
private void validateDependencyVersions() throws MojoExecutionException, MojoFailureException {
    getLog().info(Locale.getString(CompileMojo.class, "CHECK_DEPENDENCY_VERSIONS")); //$NON-NLS-1$
    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> artifacts = this.mavenHelper.getSession().getCurrentProject().getArtifactMap();
    final String sdkArtifactKey = ArtifactUtils.versionlessKey(sarlSdkGroupId, sarlSdkArtifactId);
    final Artifact sdkArtifact = artifacts.get(sdkArtifactKey);
    if (sdkArtifact != null) {
        final Map<String, ArtifactVersion> versions = new TreeMap<>();
        final Set<Artifact> dependencies = this.mavenHelper.resolveDependencies(sdkArtifactKey, false);
        for (final Artifact dependency : dependencies) {
            final ArtifactVersion dependencyVersion = new DefaultArtifactVersion(dependency.getVersion());
            final String dependencyKey = ArtifactUtils.versionlessKey(dependency);
            final ArtifactVersion currentVersion = versions.get(dependencyKey);
            if (currentVersion == null || dependencyVersion.compareTo(currentVersion) > 0) {
                versions.put(dependencyKey, dependencyVersion);
            }//www.ja v  a  2 s  .c  o m
        }

        for (final Entry<String, ArtifactVersion> entry : versions.entrySet()) {
            final Artifact dependencyArtifact = artifacts.get(entry.getKey());
            if (dependencyArtifact != null) {
                final ArtifactVersion dependencyVersion = new DefaultArtifactVersion(
                        dependencyArtifact.getVersion());
                if (entry.getValue().compareTo(dependencyVersion) > 0) {
                    final String message = Locale.getString(CompileMojo.class,
                            "INVALID_SARL_SDK_DEPENDENCY_VERSION", //$NON-NLS-1$
                            dependencyArtifact.getGroupId(), dependencyArtifact.getArtifactId(),
                            dependencyArtifact.getVersion(), entry.getValue().toString());
                    getLog().error(message);
                    hasError = true;
                }
            }
        }
    }

    if (hasError) {
        throw new MojoFailureException(
                Locale.getString(CompileMojo.class, "INVALID_SARL_SDK_DEPENDENCY_VERSION_TITLE")); //$NON-NLS-1$
    }
}

From source file:io.sarl.maven.compiler.MavenHelper.java

License:Apache License

/** Build the map of dependencies for the current plugin.
 *
 * @return the artifact.//from  w ww  .j  a v  a 2 s  .co m
 * @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;
}

From source file:io.sarl.maven.compiler.MavenHelper.java

License:Apache License

/** 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</code>
 * @throws MojoExecutionException if the plugin was not found.
 *///from w ww  . j  a  v a 2  s .c  o  m
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(Locale.getString(MavenHelper.class, "UNKNOWN_PLUGIN_VERSION", key)); //$NON-NLS-1$
    }
    throw new MojoExecutionException(
            Locale.getString(MavenHelper.class, "PLUGIN_NOT_FOUND_IN_DEPS", key, deps)); //$NON-NLS-1$
}

From source file:io.takari.maven.workspace.GenerationsWorkspaceReader.java

License:Apache License

@Inject
public GenerationsWorkspaceReader(MavenSession session) {

    String forceArtifactResolutionFromReactor = session.getSystemProperties()
            .getProperty("maven.forceArtifactResolutionFromReactor");
    if (forceArtifactResolutionFromReactor != null && forceArtifactResolutionFromReactor.equals("true")) {
        allowArtifactsWithoutAFileToBeResolvedInTheReactor = Boolean
                .parseBoolean(forceArtifactResolutionFromReactor);
    }/*from  w  w w .j a v  a2 s.  c om*/

    //
    // Right now this is only enabled for the maven-eclipse-plugin
    //
    String resolveFromWorkspaceProperty = session.getSystemProperties()
            .getProperty("maven.workspaceResolutionEnabled");
    if (resolveFromWorkspaceProperty != null && resolveFromWorkspaceProperty.equals("true")) {
        workspaceResolutionEnabled = Boolean.parseBoolean(resolveFromWorkspaceProperty);
    }

    //
    // Buildspace
    //
    buildProjects = session.getProjectMap();
    buildProjectsByGA = new HashMap<String, List<MavenProject>>();

    for (MavenProject project : buildProjects.values()) {
        String key = ArtifactUtils.versionlessKey(project.getGroupId(), project.getArtifactId());
        List<MavenProject> projects = buildProjectsByGA.get(key);
        if (projects == null) {
            projects = new ArrayList<MavenProject>(1);
            buildProjectsByGA.put(key, projects);
        }
        projects.add(project);
    }

    //
    // Workspace
    //
    workspaceProjects = getProjectMap(session.getAllProjects());
    workspaceProjectsByGA = new HashMap<String, List<MavenProject>>();

    for (MavenProject project : workspaceProjects.values()) {
        String key = ArtifactUtils.versionlessKey(project.getGroupId(), project.getArtifactId());
        List<MavenProject> projects = workspaceProjectsByGA.get(key);
        if (projects == null) {
            projects = new ArrayList<MavenProject>(1);
            workspaceProjectsByGA.put(key, projects);
        }
        projects.add(project);
    }

    repository = new WorkspaceRepository("reactor", new HashSet<String>(buildProjects.keySet()));
}

From source file:io.takari.maven.workspace.GenerationsWorkspaceReader.java

License:Apache License

public List<String> findVersions(Artifact artifact) {

    List<String> versions = new ArrayList<String>();
    String key = ArtifactUtils.versionlessKey(artifact.getGroupId(), artifact.getArtifactId());
    List<MavenProject> projects = buildProjectsByGA.get(key);

    if (projects != null) {
        for (MavenProject project : projects) {
            File artifactFile = find(project, artifact);
            if (artifactFile != null) {
                versions.add(project.getVersion());
            }/*from   ww  w. ja  v a 2  s.  c o m*/
        }
    }

    if (versions.isEmpty() && workspaceResolutionEnabled) {
        projects = workspaceProjectsByGA.get(key);
        if (projects != null) {
            for (MavenProject project : projects) {
                File artifactFile = findWorkspaceArtifact(project, artifact);
                if (artifactFile != null) {
                    versions.add(project.getVersion());
                }
            }
        }
    }

    return Collections.unmodifiableList(versions);
}

From source file:NPanday.Plugin.Msbuild.MsbuildMojo.java

License:Apache License

private void copyDependencies(Collection<Artifact> requiredArtifacts) throws MojoExecutionException {
    Map<String, MavenProject> projects = new HashMap<String, MavenProject>();
    for (MavenProject p : reactorProjects) {
        projects.put(ArtifactUtils.versionlessKey(p.getGroupId(), p.getArtifactId()), p);
    }// w w w  .jav  a 2 s .c om
    getLog().info("projects = " + projects.keySet());

    for (Object artifact : requiredArtifacts) {
        Artifact a = (Artifact) artifact;

        File targetDir;
        String vKey = ArtifactUtils.versionlessKey(a);
        if (!projects.containsKey(vKey)) {
            String path = a.getGroupId() + "/" + a.getArtifactId() + "-" + a.getBaseVersion();
            targetDir = new File(referencesDirectory, path);
        } else {
            // Likely a project reference in MSBuild.
            // If the other project was not built with MSBuild, make sure the artifact is present where it will look for it
            // Note: deliberately limited for now - will only work with reactor projects and doesn't test what are references and what are not
            File binDir = new File(projects.get(vKey).getBasedir(), "bin");
            targetDir = new File(binDir, configuration);
        }
        File targetFile = new File(targetDir, a.getArtifactId() + "." + a.getArtifactHandler().getExtension());

        getLog().info("Copying reference " + vKey + " to " + targetFile);
        if (!targetFile.exists()) {
            targetFile.getParentFile().mkdirs();

            try {
                FileUtils.copyFile(a.getFile(), targetFile);
            } catch (IOException e) {
                throw new MojoExecutionException(
                        "Error copying reference from the local repository to .references: " + e.getMessage(),
                        e);
            }
        }
    }
}

From source file:org.bsc.maven.plugin.processor.Debug.java

License:Open Source License

public void printDeps(String name, Collection<org.apache.maven.model.Dependency> dependencies) {
    System.out.println(name);/*  w w w.j  a  v  a 2  s  .c o  m*/
    for (org.apache.maven.model.Dependency d : dependencies) {

        System.out.printf("dependency [%s]\n", d.toString());

        String versionlessKey = ArtifactUtils.versionlessKey(d.getGroupId(), d.getArtifactId());

        Artifact artifact = (Artifact) project.getArtifactMap().get(versionlessKey);

        if (null != artifact) {
            File file = artifact.getFile();
            System.out.printf("artifact [%s]\n", file.getPath());
        }
    }
}

From source file:org.codehaus.mojo.animal_sniffer.maven.BuildSignaturesMojo.java

License:Open Source License

private boolean detectJavaBootClasspath(String javaExecutable)
        throws MojoFailureException, MojoExecutionException {
    getLog().info("Attempting to auto-detect the boot classpath for " + javaExecutable);
    Iterator i = pluginArtifacts.iterator();
    Artifact javaBootClasspathDetector = null;
    while (i.hasNext() && javaBootClasspathDetector == null) {
        Artifact candidate = (Artifact) i.next();

        if (StringUtils.equals(jbcpdGroupId, candidate.getGroupId())
                && StringUtils.equals(jbcpdArtifactId, candidate.getArtifactId()) && candidate.getFile() != null
                && candidate.getFile().isFile()) {
            javaBootClasspathDetector = candidate;
        }/*from   w  w w .java2  s .com*/
    }
    if (javaBootClasspathDetector == null) {
        if (skipIfNoJavaHome) {
            getLog().warn("Skipping signature generation as could not find boot classpath detector ("
                    + ArtifactUtils.versionlessKey(jbcpdGroupId, jbcpdArtifactId) + ").");
            return false;
        }
        throw new MojoFailureException("Could not find boot classpath detector ("
                + ArtifactUtils.versionlessKey(jbcpdGroupId, jbcpdArtifactId) + ").");
    }

    try {
        if (!detectJavaClasspath(javaBootClasspathDetector, javaExecutable)) {
            return false;
        }
    } catch (CommandLineException e) {
        throw new MojoExecutionException(e.getLocalizedMessage(), e);
    }
    return true;
}