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

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

Introduction

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

Prototype

public List<Artifact> getAttachedArtifacts() 

Source Link

Usage

From source file:org.axway.grapes.maven.report.ModuleBuilder.java

/**
 * Turn a maven project (Maven data model) into a module (Grapes data model)
 *
 * @param project MavenProject/* w  ww .  j  ava  2 s  .  c  om*/
 * @return Module
 */
public Module getModule(final MavenProject project, final LicenseResolver licenseResolver,
        final ArtifactResolver artifactResolver) throws MojoExecutionException {

    final Module module = GrapesTranslator.getGrapesModule(project);
    final List<License> licenses = licenseResolver.resolve(project);

    /* Manage Artifacts */
    final Artifact mainArtifact = GrapesTranslator.getGrapesArtifact(project.getArtifact());
    addLicenses(mainArtifact, licenses);
    module.addArtifact(mainArtifact);

    // Get pom file if main artifact is not already a pom file
    if (!mainArtifact.getType().equals("pom")) {
        final Artifact pomArtifact = GrapesTranslator.getGrapesArtifact(project.getModel());
        addLicenses(pomArtifact, licenses);
        module.addArtifact(pomArtifact);
    }

    for (int i = 0; i < project.getAttachedArtifacts().size(); i++) {
        artifactResolver.resolveArtifact(project, project.getAttachedArtifacts().get(i));
        final Artifact attachedArtifact = GrapesTranslator
                .getGrapesArtifact(project.getAttachedArtifacts().get(i));
        // handle licenses
        addLicenses(attachedArtifact, licenses);
        module.addArtifact(attachedArtifact);
    }

    /* Manage Dependencies */
    for (int i = 0; i < project.getDependencies().size(); i++) {
        final Dependency dependency = GrapesTranslator.getGrapesDependency(
                artifactResolver.resolveArtifact(project, project.getDependencies().get(i)),
                project.getDependencies().get(i).getScope());

        // handle licenses
        for (License license : licenseResolver.resolve(project, dependency.getTarget().getGroupId(),
                dependency.getTarget().getArtifactId(), dependency.getTarget().getVersion())) {
            dependency.getTarget().addLicense(license.getName());
        }

        module.addDependency(dependency);
    }

    return module;
}

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

License:Apache License

/**
 * Saves all attributes.//from   www.  jav a  2s  .c o m
 * @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.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);
    }/* w  ww . j av  a2  s  .c om*/
    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;
}

From source file:org.commonjava.sjbi.maven3.builder.M3BuildResult.java

License:Open Source License

M3BuildResult(final MavenExecutionResult mavenResult) {
    if (mavenResult.hasExceptions()) {
        setErrors(mavenResult.getExceptions());
    }/*w  w  w.  ja v a  2 s. c  o m*/

    final List<ArtifactSetRef> ars = new ArrayList<ArtifactSetRef>();
    for (final MavenProject project : mavenResult.getTopologicallySortedProjects()) {
        final ProjectRef pr = new ProjectRef(project.getGroupId(), project.getArtifactId(),
                project.getVersion());
        pr.setPomFile(project.getFile());

        final ArtifactSetRef ar = new ArtifactSetRef(pr);

        final Artifact mainArtifact = project.getArtifact();
        ar.addArtifactRef(mainArtifact.getType(), mainArtifact.getClassifier(), mainArtifact.getFile());

        for (final Artifact a : project.getAttachedArtifacts()) {
            ar.addArtifactRef(a.getType(), a.getClassifier(), a.getFile());
        }

        ars.add(ar);
    }

    setArtifactSets(ars);
}

From source file:org.debian.dependency.ProjectArtifactSpy.java

License:Apache License

@Override
public void onEvent(final Object event) {
    if (!(event instanceof ExecutionEvent)) {
        return;//from  w  ww  . ja  v  a 2  s.c o  m
    }
    ExecutionEvent execEvent = (ExecutionEvent) event;
    if (!Type.ProjectSucceeded.equals(execEvent.getType())
            && !Type.ForkedProjectSucceeded.equals(execEvent.getType())) {
        return;
    }

    MavenProject project = execEvent.getProject();
    Artifact pomArtifact = repoSystem.createProjectArtifact(project.getGroupId(), project.getArtifactId(),
            project.getVersion());
    pomArtifact.setFile(project.getFile());

    // the first project should always be the top-level project
    if (outputFile == null) {
        outputFile = new File(project.getBuild().getDirectory(), ServicePackage.PROJECT_ARTIFACT_REPORT_NAME);
    }

    recordArtifact(pomArtifact);
    recordArtifact(project.getArtifact());
    for (Artifact artifact : project.getAttachedArtifacts()) {
        recordArtifact(artifact);
    }
}

From source file:org.eclipse.tycho.plugins.p2.BaselineValidator.java

License:Open Source License

public Map<String, IP2Artifact> validateAndReplace(MavenProject project,
        Map<String, IP2Artifact> reactorMetadata, List<Repository> baselineRepositories,
        BaselineMode baselineMode, BaselineReplace baselineReplace) throws IOException, MojoExecutionException {

    Map<String, IP2Artifact> result = reactorMetadata;

    if (baselineMode != disable && baselineRepositories != null && !baselineRepositories.isEmpty()) {
        List<MavenRepositoryLocation> _repositories = new ArrayList<MavenRepositoryLocation>();
        for (Repository repository : baselineRepositories) {
            if (repository.getUrl() != null) {
                _repositories.add(new MavenRepositoryLocation(repository.getId(), repository.getUrl()));
            }/*  www. ja v  a2s  .  c om*/
        }

        File baselineBasedir = new File(project.getBuild().getDirectory(), "baseline");

        BaselineService baselineService = getService(BaselineService.class);

        Map<String, IP2Artifact> baselineMetadata = baselineService.getProjectBaseline(_repositories,
                reactorMetadata, baselineBasedir);

        if (baselineMetadata != null) {
            CompoundArtifactDelta delta = getDelta(baselineService, baselineMetadata, reactorMetadata);
            if (delta != null) {
                if (System.getProperties().containsKey("tycho.debug.artifactcomparator")) {
                    File logdir = new File(project.getBuild().getDirectory(), "artifactcomparison");
                    log.info("Artifact comparison detailed log directory " + logdir.getAbsolutePath());
                    for (Map.Entry<String, ArtifactDelta> classifier : delta.getMembers().entrySet()) {
                        if (classifier.getValue() instanceof CompoundArtifactDelta) {
                            ((CompoundArtifactDelta) classifier.getValue())
                                    .writeDetails(new File(logdir, classifier.getKey()));
                        }
                    }
                }
                if (baselineMode == fail || (baselineMode == failCommon && !isMissingOnlyDelta(delta))) {
                    throw new MojoExecutionException(delta.getDetailedMessage());
                } else {
                    String message = log.isDebugEnabled() ? delta.getDetailedMessage() : delta.getMessage();
                    log.warn(project.toString() + ": " + message);
                }
            }

            if (baselineReplace != none) {
                result = new LinkedHashMap<String, IP2Artifact>();

                // replace reactor artifacts with baseline
                ArrayList<String> replaced = new ArrayList<String>();
                for (Map.Entry<String, IP2Artifact> artifact : baselineMetadata.entrySet()) {
                    String classifier = artifact.getKey();
                    FileUtils.copyFile(artifact.getValue().getLocation(),
                            reactorMetadata.get(classifier).getLocation());
                    result.put(classifier, artifact.getValue());
                    if (classifier != null) {
                        replaced.add(classifier);
                    }
                }

                // un-attach and delete artifacts present in reactor but not in baseline
                ArrayList<String> removed = new ArrayList<String>();
                ArrayList<String> inconsistent = new ArrayList<String>();
                for (Map.Entry<String, IP2Artifact> entry : reactorMetadata.entrySet()) {
                    String classifier = entry.getKey();
                    IP2Artifact artifact = entry.getValue();
                    if (classifier == null || artifact == null) {
                        continue;
                    }
                    if (baselineReplace == all && !baselineMetadata.containsKey(classifier)) {
                        List<Artifact> attachedArtifacts = project.getAttachedArtifacts();
                        ListIterator<Artifact> iterator = attachedArtifacts.listIterator();
                        while (iterator.hasNext()) {
                            if (classifier.equals(iterator.next().getClassifier())) {
                                iterator.remove();
                                break;
                            }
                        }
                        artifact.getLocation().delete();
                        removed.add(classifier);
                    } else {
                        inconsistent.add(classifier);
                        result.put(classifier, artifact);
                    }
                }

                // Reactor build can have more or less artifacts than baseline 
                // baselineReplace==all guarantees consistency of build artifacts with baseline repository
                // baselineReplace==none build results are self-consistent, but maybe inconsistent with baseline
                // baselineReplace==common build artifacts are inconsistent

                if (log.isInfoEnabled()) {
                    StringBuilder msg = new StringBuilder();
                    msg.append(project.toString());
                    msg.append("\n    The main artifact has been replaced with the baseline version.\n");
                    if (!replaced.isEmpty()) {
                        msg.append(
                                "    The following attached artifacts have been replaced with the baseline version: ");
                        msg.append(replaced.toString());
                        msg.append("\n");
                    }
                    if (!removed.isEmpty()) {
                        msg.append(
                                "    The following attached artifacts are not present in the baseline and have been removed: ");
                        msg.append(removed.toString());
                        msg.append("\n");
                    }
                    log.info(msg.toString());
                }
            }
        } else {
            log.info("No baseline version " + project);
        }
    }
    return result;
}

From source file:org.eclipse.tycho.plugins.p2.P2MetadataMojo.java

License:Open Source License

/**
 * Returns a map from classifiers to artifact files of the given project. The classifier
 * <code>null</code> is mapped to the project's main artifact.
 *///ww w  .ja v a  2  s.c  om
private static Map<String, File> getAllProjectArtifacts(MavenProject project) {
    Map<String, File> artifacts = new HashMap<String, File>();
    Artifact mainArtifact = project.getArtifact();
    if (mainArtifact != null) {
        artifacts.put(null, mainArtifact.getFile());
    }
    for (Artifact attachedArtifact : project.getAttachedArtifacts()) {
        artifacts.put(attachedArtifact.getClassifier(), attachedArtifact.getFile());
    }
    return artifacts;
}

From source file:org.eclipse.tycho.target.TargetPlatformMojo.java

License:Open Source License

private static File getAttachedArtifact(MavenProject project, String classifier) {
    for (Artifact artifact : project.getAttachedArtifacts()) {
        if (classifier.equals(artifact.getClassifier())) {
            return artifact.getFile();
        }//from  ww  w  .  ja  v a  2s  . co  m
    }
    return null;
}

From source file:org.elasticsearch.enforcer.rules.BannedAttachedArtifacts.java

License:Apache License

public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException {
    Log log = helper.getLog();/*from  w  ww  .ja  v  a  2  s. c  om*/

    List<Pattern> excludedPatterns = new ArrayList<Pattern>(excludes.size());
    for (String exclude : excludes) {
        excludedPatterns.add(Pattern.compile(exclude));
    }

    try {
        MavenProject project = (MavenProject) helper.evaluate("${project}");
        List<Artifact> artifactList = project.getAttachedArtifacts();
        for (Artifact artifact : artifactList) {
            String artifactFileName = artifact.getFile().getName();
            log.debug("evaluating artifact [" + artifactFileName + "]");
            for (Pattern pattern : excludedPatterns) {
                if (pattern.matcher(artifactFileName).matches()) {
                    throw new EnforcerRuleException("found banned attached artifact: artifact ["
                            + artifactFileName + "] matched exclude pattern [" + pattern.toString() + "]");
                }
            }
        }

    } catch (ExpressionEvaluationException e) {
        throw new EnforcerRuleException("Unable to lookup expression", e);
    }

}

From source file:org.fluidity.deployment.maven.DependenciesSupportImpl.java

License:Apache License

@Override
public void saveArtifact(final MavenProject project, final File file, final String finalName,
        final String classifier, final String packaging, final Logger log) throws MojoExecutionException {
    final boolean unclassified = classifier == null || classifier.isEmpty();
    final String outputName = unclassified ? String.format("%s.%s", finalName, packaging)
            : String.format("%s-%s.%s", finalName, classifier, packaging);

    final File outputFile = new File(outputName);
    final String outputPath = outputFile.getAbsolutePath();

    final Artifact artifact = project.getArtifact();
    assert artifact != null;

    final File artifactFile = artifact.getFile();

    if (artifactFile != null && artifactFile.getAbsolutePath().equals(outputPath)) {
        log.info(String.format("Replacing %s: %s", packaging, artifactFile.getAbsolutePath()));

        if (!artifactFile.delete()) {
            throw new MojoExecutionException(String.format("Could not delete %s", artifactFile));
        }//from w w w. j  av a2s.c  o  m

        if (!file.renameTo(artifactFile)) {
            throw new MojoExecutionException(String.format("Could not create %s", artifactFile));
        }
    } else {
        boolean replacing = false;

        for (final Artifact attached : project.getAttachedArtifacts()) {
            if (attached.getFile().getAbsolutePath().equals(outputPath)) {
                replacing = true;
                break;
            }
        }

        log.info(String.format("%s %s: %s", replacing ? "Replacing" : "Saving", packaging, outputPath));

        if (outputFile.exists() && !outputFile.delete()) {
            throw new MojoExecutionException(String.format("Could not delete %s", outputFile));
        }

        if (!file.renameTo(outputFile)) {
            throw new MojoExecutionException(String.format("Could not create %s", outputFile));
        }

        if (!replacing) {
            final DefaultArtifact attachment = new DefaultArtifact(project.getGroupId(),
                    project.getArtifactId(), project.getVersion(), artifact.getScope(), packaging, classifier,
                    artifact.getArtifactHandler());
            attachment.setFile(outputFile);

            project.addAttachedArtifact(attachment);
        }
    }
}