Example usage for org.apache.maven.project MavenProject getArtifact

List of usage examples for org.apache.maven.project MavenProject getArtifact

Introduction

In this page you can find the example usage for org.apache.maven.project MavenProject getArtifact.

Prototype

public Artifact getArtifact() 

Source Link

Usage

From source file:org.basepom.mojo.duplicatefinder.artifact.ArtifactFileResolver.java

License:Apache License

public ArtifactFileResolver(final MavenProject project, final boolean preferLocal)
        throws DependencyResolutionRequiredException {
    checkNotNull(project, "project is null");
    this.preferLocal = preferLocal;

    this.localArtifactCache = HashBiMap.create(project.getArtifacts().size());
    this.repoArtifactCache = HashBiMap.create(project.getArtifacts().size());

    for (final Artifact artifact : project.getArtifacts()) {
        final File repoPath = artifact.getFile();
        final Artifact canonicalizedArtifact = ArtifactFileResolver.canonicalizeArtifact(artifact);

        checkState(repoPath != null && repoPath.exists(), "Repository Path '%s' does not exist.", repoPath);
        final File oldFile = repoArtifactCache.put(canonicalizedArtifact, repoPath);
        checkState(oldFile == null || oldFile.equals(repoPath), "Already encountered a file for %s: %s",
                canonicalizedArtifact, oldFile);
    }//  www .  ja  va  2  s  .  c o m

    for (final MavenProject referencedProject : project.getProjectReferences().values()) {
        // referenced projects only have GAV coordinates but no scope.
        final Set<Artifact> repoArtifacts = findRepoArtifacts(referencedProject, repoArtifactCache);
        checkState(!repoArtifacts.isEmpty(), "Found project reference to %s but no repo reference!",
                referencedProject.getArtifact());

        for (final Artifact artifact : repoArtifacts) {

            final File outputDir = isTestArtifact(artifact) ? getTestOutputDirectory(referencedProject)
                    : getOutputDirectory(referencedProject);

            if (outputDir != null && outputDir.exists()) {
                final File oldFile = localArtifactCache.put(artifact, outputDir);
                checkState(oldFile == null || oldFile.equals(outputDir),
                        "Already encountered a file for %s: %s", artifact, oldFile);
            }
        }
    }
}

From source file:org.basepom.mojo.duplicatefinder.artifact.ArtifactFileResolver.java

License:Apache License

private static Set<Artifact> findRepoArtifacts(final MavenProject project,
        final Map<Artifact, File> repoArtifactCache) {
    final ImmutableSet.Builder<Artifact> builder = ImmutableSet.builder();

    for (final Artifact artifact : repoArtifactCache.keySet()) {
        if (Objects.equal(project.getArtifact().getGroupId(), artifact.getGroupId())
                && Objects.equal(project.getArtifact().getArtifactId(), artifact.getArtifactId())
                && Objects.equal(project.getArtifact().getBaseVersion(), artifact.getBaseVersion())) {
            builder.add(artifact);//from w w w.j  a  v  a 2s  . c o  m
        }
    }
    return builder.build();
}

From source file:org.basepom.mojo.duplicatefinder.classpath.ClasspathDescriptor.java

License:Apache License

public static ClasspathDescriptor createClasspathDescriptor(final MavenProject project,
        final Map<File, Artifact> fileToArtifactMap, final Collection<String> ignoredResourcePatterns,
        final Collection<MavenCoordinates> ignoredDependencies, final boolean useDefaultResourceIgnoreList,
        final File[] projectFolders) throws MojoExecutionException, InvalidVersionSpecificationException {
    checkNotNull(project, "project is null");
    checkNotNull(fileToArtifactMap, "fileToArtifactMap is null");
    checkNotNull(ignoredResourcePatterns, "ignoredResourcePatterns is null");
    checkNotNull(ignoredDependencies, "ignoredDependencies is null");
    checkNotNull(projectFolders, "projectFolders is null");

    final Artifact projectArtifact = project.getArtifact();

    final ClasspathDescriptor classpathDescriptor = new ClasspathDescriptor(useDefaultResourceIgnoreList,
            ignoredResourcePatterns);//from w w w  . j a  v  a2s . c o m
    final MatchArtifactPredicate matchArtifactPredicate = new MatchArtifactPredicate(ignoredDependencies);

    Artifact artifact = null;
    File file = null;

    try {
        // any entry is either a jar in the repo or a folder in the target folder of a referenced
        // project. Add the elements that are not ignored by the ignoredDependencies predicate to
        // the classpath descriptor.
        for (final Map.Entry<File, Artifact> entry : fileToArtifactMap.entrySet()) {
            artifact = entry.getValue();
            file = entry.getKey();

            if (file.exists()) {
                // Add to the classpath if the artifact predicate does not apply (then it is not in the ignoredDependencies list).
                if (!matchArtifactPredicate.apply(artifact)) {
                    classpathDescriptor.addClasspathElement(file, artifact);
                }
            } else {
                // e.g. when running the goal explicitly on a cleaned multi-module project, referenced
                // projects will try to use the output folders of a referenced project but these do not
                // exist. Obviously, in this case the plugin might return incorrect results (unfortunately
                // false negatives, but there is not much it can do here (besides fail the build here with a
                // cryptic error message. Maybe add a flag?).
                LOG.debug("Classpath element '%s' does not exist.", file.getAbsolutePath());
            }
        }
    } catch (final IOException ex) {
        throw new MojoExecutionException(
                format("Error trying to access file '%s' for artifact '%s'", file, artifact), ex);
    }

    try {
        // Add project folders unconditionally.
        for (final File projectFile : projectFolders) {
            file = projectFile;
            if (projectFile.exists()) {
                classpathDescriptor.addClasspathElement(file, projectArtifact);
            } else {
                // See above. This may happen in the project has been cleaned before running the goal directly.
                LOG.debug("Project folder '%s' does not exist.", file.getAbsolutePath());
            }
        }
    } catch (final IOException ex) {
        throw new MojoExecutionException(format("Error trying to access project folder '%s'", file), ex);
    }

    return classpathDescriptor;
}

From source file:org.basepom.mojo.duplicatefinder.XMLWriterUtils.java

License:Apache License

public static void addProjectInformation(SMOutputElement rootElement, MavenProject project)
        throws XMLStreamException {
    SMOutputElement projectElement = rootElement.addElement("project");
    addAttribute(projectElement, "artifactId", project.getArtifact().getArtifactId());
    addAttribute(projectElement, "groupId", project.getArtifact().getGroupId());
    addAttribute(projectElement, "version", project.getArtifact().getVersion());
    addAttribute(projectElement, "classifier", project.getArtifact().getClassifier());
    addAttribute(projectElement, "type", project.getArtifact().getType());
}

From source file:org.codehaus.cargo.maven2.DependencyCalculator.java

License:Apache License

/**
 * Execute the dependency calculator.//from   ww  w  . j a  v  a 2  s.co  m
 * @return List of dependency files.
 * @throws Exception If anything goes wrong.
 */
public Set<File> execute() throws Exception {
    ProfileManager profileManager = new DefaultProfileManager(container);

    fixupProjectArtifact();

    // Calculate the new deps
    Artifact art = mavenProject.getArtifact();
    Artifact art2 = artifactFactory.createArtifactWithClassifier(art.getGroupId() + ".cargodeps",
            art.getArtifactId(), art.getVersion(), "pom", null);
    resolver.resolve(art2, remoteRepositories, localRepository);

    MavenProject mavenProject = mavenProjectBuilder.buildWithDependencies(art2.getFile(), localRepository,
            profileManager);

    Set<File> filesToAdd = new HashSet<File>();

    for (Object artifact : mavenProject.getArtifacts()) {
        Artifact artdep = (Artifact) artifact;
        if (artdep.getType().equals("jar")) {
            resolver.resolve(artdep, remoteRepositories, localRepository);
            filesToAdd.add(artdep.getFile());
        }
    }

    return filesToAdd;
}

From source file:org.codehaus.cargo.maven2.DependencyCalculator.java

License:Apache License

/**
 * Fixup the project artifact./*from ww w. j  a v  a 2 s  .  c o  m*/
 * @throws Exception If anything goes wrong.
 */
protected void fixupProjectArtifact() throws Exception {
    MavenProject mp2 = new MavenProject(mavenProject);
    // For each of our dependencies..
    for (Object artifact : mp2.createArtifacts(artifactFactory, null, null)) {
        Artifact art = (Artifact) artifact;
        if (art.getType().equals("war")) {
            // Sigh...
            Artifact art2 = artifactFactory.createArtifactWithClassifier(art.getGroupId(), art.getArtifactId(),
                    art.getVersion(), "pom", null);
            fixupRepositoryArtifact(art2);
        }
    }

    // If we mess with this model, it's the 'REAL' model. So lets copy it

    Model pomFile = mp2.getModel();

    File outFile = File.createTempFile("pom", ".xml");
    MavenXpp3Writer pomWriter = new MavenXpp3Writer();

    pomWriter.write(new FileWriter(outFile), pomFile);

    MavenXpp3Reader pomReader = new MavenXpp3Reader();
    pomFile = pomReader.read(new FileReader(outFile));

    Artifact art = mp2.getArtifact();
    fixModelAndSaveInRepository(art, pomFile);
    outFile.delete();
}

From source file:org.codehaus.cargo.maven2.util.CargoProject.java

License:Apache License

/**
 * Saves all attributes./*w w  w  .jav  a 2  s .com*/
 * @param project Maven2 project.
 * @param log Logger.
 */
public CargoProject(MavenProject project, Log log) {
    this(project.getPackaging(), project.getGroupId(), project.getArtifactId(),
            project.getBuild().getDirectory(), project.getBuild().getFinalName(), project.getArtifact(),
            project.getAttachedArtifacts(), project.getArtifacts(), log);
}

From source file:org.codehaus.mojo.appassembler.daemon.generic.GenericDaemonGenerator.java

License:Open Source License

private Daemon createDaemon(MavenProject project, ArtifactRepositoryLayout layout) {
    Daemon complete = new Daemon();

    complete.setClasspath(new Classpath());

    // -----------------------------------------------------------------------
    // Add the project itself as a dependency.
    // -----------------------------------------------------------------------
    Dependency projectDependency = new Dependency();
    Artifact projectArtifact = project.getArtifact();
    projectArtifact.isSnapshot();//  w w  w . ja  va  2 s  .  c  om
    projectDependency.setGroupId(projectArtifact.getGroupId());
    projectDependency.setArtifactId(projectArtifact.getArtifactId());
    projectDependency.setVersion(projectArtifact.getVersion());
    projectDependency.setClassifier(projectArtifact.getClassifier());
    projectDependency.setRelativePath(layout.pathOf(projectArtifact));
    complete.getClasspath().addDependency(projectDependency);

    // -----------------------------------------------------------------------
    // Add all the dependencies of the project.
    // -----------------------------------------------------------------------
    for (Iterator it = project.getRuntimeArtifacts().iterator(); it.hasNext();) {
        Artifact artifact = (Artifact) it.next();

        artifact.isSnapshot();

        Dependency dependency = new Dependency();
        dependency.setGroupId(artifact.getGroupId());
        dependency.setArtifactId(artifact.getArtifactId());
        dependency.setVersion(artifact.getVersion());

        dependency.setClassifier(artifact.getClassifier());
        dependency.setRelativePath(layout.pathOf(artifact));

        complete.getClasspath().addDependency(dependency);
    }

    return complete;
}

From source file:org.codehaus.mojo.appassembler.daemon.jsw.JavaServiceWrapperDaemonGenerator.java

License:Open Source License

private void createClasspath(Daemon daemon, DaemonGenerationRequest request, FormattedProperties confFile,
        Properties configuration) {
    final String wrapperClassPathPrefix = "wrapper.java.classpath.";

    int counter = 1;
    confFile.setProperty(wrapperClassPathPrefix + counter++, "lib/wrapper.jar");

    String configurationDirFirst = configuration.getProperty("configuration.directory.in.classpath.first");
    if (configurationDirFirst != null) {
        confFile.setProperty(wrapperClassPathPrefix + counter++, configurationDirFirst);
    }//from   w ww .  j av a2  s .com

    MavenProject project = request.getMavenProject();
    ArtifactRepositoryLayout layout = request.getRepositoryLayout();

    if (daemon.isUseWildcardClassPath()) {
        confFile.setProperty(wrapperClassPathPrefix + counter++, "%REPO_DIR%/*");
    } else {
        confFile.setProperty(wrapperClassPathPrefix + counter++,
                "%REPO_DIR%/" + createDependency(layout, project.getArtifact(), true).getRelativePath());

        Iterator j = project.getRuntimeArtifacts().iterator();
        while (j.hasNext()) {
            Artifact artifact = (Artifact) j.next();

            confFile.setProperty(wrapperClassPathPrefix + counter,
                    "%REPO_DIR%/"
                            + createDependency(layout, artifact, daemon.isUseTimestampInSnapshotFileName())
                                    .getRelativePath());
            counter++;
        }
    }

    String configurationDirLast = configuration.getProperty("configuration.directory.in.classpath.last");
    if (configurationDirLast != null) {
        confFile.setProperty(wrapperClassPathPrefix + counter++, configurationDirLast);
    }
}

From source file:org.codehaus.mojo.chronos.jmeter.DependencyUtil.java

License:LGPL

public List getDependencies(MavenProject project) {
    List result = new ArrayList();

    Iterator it = project.getAttachedArtifacts().iterator();
    while (it.hasNext()) {
        Artifact artifact = (Artifact) it.next();
        File attachedArtifactFile = artifact.getFile();
        result.add(attachedArtifactFile);
    }//from  w ww.  j  a  v  a 2  s .  c  o m
    File artifactFile = project.getArtifact().getFile();
    if (artifactFile == null) {
        log.warn("Artifact not found. Note that if Your JMeter test contains JUnittestcases, "
                + "You can only invoke this goal through the default lifecycle.");
    } else {
        result.add(artifactFile);
    }
    Set dependencyArtifacts = project.getArtifacts();
    if (dependencyArtifacts != null) {
        Iterator deps = dependencyArtifacts.iterator();
        while (deps.hasNext()) {
            Artifact dependency = (Artifact) deps.next();
            result.add(dependency.getFile());
        }
    }
    return result;
}