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

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

Introduction

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

Prototype

public Set<Artifact> getArtifacts() 

Source Link

Document

All dependencies that this project has, including transitive ones.

Usage

From source file:org.vafer.jdeb.maven.DebMojo.java

License:Apache License

/**
 * Main entry point//from w w  w . j a va  2s . com
 *
 * @throws MojoExecutionException on error
 */
@Override
public void execute() throws MojoExecutionException {

    final MavenProject project = getProject();

    if (skip) {
        getLog().info("skipping as configured (skip)");
        return;
    }

    if (skipPOMs && isPOM()) {
        getLog().info("skipping because artifact is a pom (skipPOMs)");
        return;
    }

    if (skipSubmodules && isSubmodule()) {
        getLog().info("skipping submodule (skipSubmodules)");
        return;
    }

    setData(dataSet);

    console = new MojoConsole(getLog(), verbose);

    initializeSignProperties();

    final VariableResolver resolver = initializeVariableResolver(new HashMap<String, String>());

    final File debFile = new File(Utils.replaceVariables(resolver, deb, openReplaceToken, closeReplaceToken));
    final File controlDirFile = new File(
            Utils.replaceVariables(resolver, controlDir, openReplaceToken, closeReplaceToken));
    final File installDirFile = new File(
            Utils.replaceVariables(resolver, installDir, openReplaceToken, closeReplaceToken));
    final File changesInFile = new File(
            Utils.replaceVariables(resolver, changesIn, openReplaceToken, closeReplaceToken));
    final File changesOutFile = new File(
            Utils.replaceVariables(resolver, changesOut, openReplaceToken, closeReplaceToken));
    final File changesSaveFile = new File(
            Utils.replaceVariables(resolver, changesSave, openReplaceToken, closeReplaceToken));
    final File keyringFile = keyring == null ? null
            : new File(Utils.replaceVariables(resolver, keyring, openReplaceToken, closeReplaceToken));

    // if there are no producers defined we try to use the artifacts
    if (dataProducers.isEmpty()) {

        if (hasMainArtifact()) {
            Set<Artifact> artifacts = new HashSet<Artifact>();

            artifacts.add(project.getArtifact());

            @SuppressWarnings("unchecked")
            final Set<Artifact> projectArtifacts = project.getArtifacts();

            for (Artifact artifact : projectArtifacts) {
                artifacts.add(artifact);
            }

            @SuppressWarnings("unchecked")
            final List<Artifact> attachedArtifacts = project.getAttachedArtifacts();

            for (Artifact artifact : attachedArtifacts) {
                artifacts.add(artifact);
            }

            for (Artifact artifact : artifacts) {
                final File file = artifact.getFile();
                if (file != null) {
                    dataProducers.add(new DataProducer() {
                        @Override
                        public void produce(final DataConsumer receiver) {
                            try {
                                final File path = new File(installDirFile.getPath(), file.getName());
                                final String entryName = path.getPath();

                                final boolean symbolicLink = SymlinkUtils.isSymbolicLink(path);
                                final TarArchiveEntry e;
                                if (symbolicLink) {
                                    e = new TarArchiveEntry(entryName, TarConstants.LF_SYMLINK);
                                    e.setLinkName(SymlinkUtils.readSymbolicLink(path));
                                } else {
                                    e = new TarArchiveEntry(entryName, true);
                                }

                                e.setUserId(0);
                                e.setGroupId(0);
                                e.setUserName("root");
                                e.setGroupName("root");
                                e.setMode(TarEntry.DEFAULT_FILE_MODE);
                                e.setSize(file.length());

                                receiver.onEachFile(new FileInputStream(file), e);
                            } catch (Exception e) {
                                getLog().error(e);
                            }
                        }
                    });
                } else {
                    getLog().error("No file for artifact " + artifact);
                }
            }
        }
    }

    try {
        DebMaker debMaker = new DebMaker(console, dataProducers, conffileProducers);
        debMaker.setDeb(debFile);
        debMaker.setControl(controlDirFile);
        debMaker.setPackage(getProject().getArtifactId());
        debMaker.setDescription(getProject().getDescription());
        debMaker.setHomepage(getProject().getUrl());
        debMaker.setChangesIn(changesInFile);
        debMaker.setChangesOut(changesOutFile);
        debMaker.setChangesSave(changesSaveFile);
        debMaker.setCompression(compression);
        debMaker.setKeyring(keyringFile);
        debMaker.setKey(key);
        debMaker.setPassphrase(passphrase);
        debMaker.setSignPackage(signPackage);
        debMaker.setSignMethod(signMethod);
        debMaker.setSignRole(signRole);
        debMaker.setResolver(resolver);
        debMaker.setOpenReplaceToken(openReplaceToken);
        debMaker.setCloseReplaceToken(closeReplaceToken);
        debMaker.validate();
        debMaker.makeDeb();

        // Always attach unless explicitly set to false
        if ("true".equalsIgnoreCase(attach)) {
            console.info("Attaching created debian package " + debFile);
            if (!isType()) {
                projectHelper.attachArtifact(project, type, classifier, debFile);
            } else {
                project.getArtifact().setFile(debFile);
            }
        }

    } catch (PackagingException e) {
        getLog().error("Failed to create debian package " + debFile, e);
        throw new MojoExecutionException("Failed to create debian package " + debFile, e);
    }

    if (!isBlank(propertyPrefix)) {
        project.getProperties().put(propertyPrefix + "version", getProjectVersion());
        project.getProperties().put(propertyPrefix + "deb", debFile.getAbsolutePath());
        project.getProperties().put(propertyPrefix + "deb.name", debFile.getName());
        project.getProperties().put(propertyPrefix + "changes", changesOutFile.getAbsolutePath());
        project.getProperties().put(propertyPrefix + "changes.name", changesOutFile.getName());
        project.getProperties().put(propertyPrefix + "changes.txt", changesSaveFile.getAbsolutePath());
        project.getProperties().put(propertyPrefix + "changes.txt.name", changesSaveFile.getName());
    }

}

From source file:org.whitesource.jenkins.maven.MavenDependenciesRecorder.java

License:Apache License

/**
 * Mojos perform different dependency resolution, so we add dependencies for each mojo.
 *//*from  w  w w  .j a  v  a2 s  .c  om*/
@Override
public boolean postExecute(MavenBuildProxy build, MavenProject pom, MojoInfo mojo, BuildListener listener,
        Throwable error) {
    recordMavenDependencies(pom.getArtifacts());
    return true;
}

From source file:org.wildfly.build.plugin.MavenProjectArtifactResolver.java

License:Apache License

public MavenProjectArtifactResolver(MavenProject mavenProject) {
    this.artifactMap = new HashMap<>();
    for (org.apache.maven.artifact.Artifact mavenProjectArtifact : mavenProject.getArtifacts()) {
        final Artifact artifact = new Artifact(mavenProjectArtifact.getGroupId(),
                mavenProjectArtifact.getArtifactId(), mavenProjectArtifact.getClassifier(),
                mavenProjectArtifact.getType(), mavenProjectArtifact.getVersion());
        StringBuilder sb = new StringBuilder();
        sb.append(artifact.getGACE().getGroupId());
        sb.append(':');
        sb.append(artifact.getGACE().getArtifactId());
        if (artifact.getGACE().getClassifier() != null && !artifact.getGACE().getClassifier().isEmpty()) {
            artifactMap.put(sb.append("::").append(artifact.getGACE().getClassifier()).toString(), artifact);
        } else {/* w w  w .j av  a2s .c o  m*/
            artifactMap.put(sb.toString(), artifact);
        }
    }
}

From source file:org.wildfly.maven.plugins.licenses.DependenciesResolver.java

public <R> SortedMap<String, R> loadDependenciesAndConvertThem(MavenProject project,
        MavenProjectDependenciesConfiguration configuration, ArtifactRepository localRepository,
        List<ArtifactRepository> remoteRepositories, SortedMap<String, MavenProject> cache,
        Function<MavenProject, R> convertFunction) {

    Pattern includedGroupPattern = (isEmpty(configuration.includedGroups) ? null
            : Pattern.compile(configuration.includedGroups));
    Pattern includedArtifactPattern = (isEmpty(configuration.includedArtifacts) ? null
            : Pattern.compile(configuration.includedArtifacts));

    Pattern excludedGroupPattern = (isEmpty(configuration.excludedGroups) ? null
            : Pattern.compile(configuration.excludedGroups));
    Pattern excludedArtifactPattern = (isEmpty(configuration.excludedArtifacts) ? null
            : Pattern.compile(configuration.excludedArtifacts));

    boolean matchInclusions = includedGroupPattern != null || includedArtifactPattern != null;
    boolean matchExclusions = excludedGroupPattern != null || excludedArtifactPattern != null;

    Set<?> depArtifacts;/*from w  w w  .ja  va  2s.  co  m*/

    if (configuration.includeTransitiveDependencies) {
        // All project dependencies
        depArtifacts = project.getArtifacts();
    } else {
        // Only direct project dependencies
        depArtifacts = project.getDependencyArtifacts();
    }

    List<String> includedScopes = configuration.includedScopes;
    List<String> excludeScopes = configuration.excludedScopes;

    boolean verbose = configuration.verbose;

    SortedMap<String, R> result = new TreeMap<String, R>();
    Logger log = getLogger();

    for (Object o : depArtifacts) {
        Artifact artifact = (Artifact) o;
        String id = getArtifactId(artifact);

        if (verbose) {
            log.info("detected artifact " + id);
        }

        String scope = artifact.getScope();
        if (isNotEmptyCollection(includedScopes) && !includedScopes.contains(scope)) {
            if (verbose) {
                log.info("skip artifact " + id + " - not in included scopes");
            }
            continue;
        }

        if (excludeScopes.contains(scope)) {
            if (verbose) {
                log.info("skip artifact " + id + " - in excluded scope " + scope);
            }
            continue;
        }

        // Check if the project should be included
        boolean isToInclude = matchesIncluded(artifact, includedGroupPattern, includedArtifactPattern);
        if (!isToInclude) {
            if (verbose) {
                log.info("skip artifact " + id + " - not in included artifactId / groupId patterns");
            }
            continue;
        }
        // Check if the project should be excluded
        boolean isToExclude = matchesExcluded(artifact, excludedGroupPattern, excludedArtifactPattern);

        if (isToExclude) {
            if (verbose) {
                log.info("skip artifact " + id + " - in excluded artifactId / groupId patterns");
            }
            continue;
        }

        MavenProject depMavenProject = getDependencyMavenProject(localRepository, remoteRepositories, cache,
                verbose, artifact, log, id);
        if (depMavenProject != null) {
            // keep the project
            result.put(id, convertFunction.apply(depMavenProject));
        }
    }
    return result;
}

From source file:org.wildfly.swarm.plugin.FractionListMojo.java

License:Apache License

public void execute() throws MojoExecutionException, MojoFailureException {

    List<Dependency> dependencies = this.project.getDependencyManagement().getDependencies();

    List<Dependency> fractionsDependencies = new ArrayList<>();

    for (Dependency dependency : dependencies) {
        if (isFraction(dependency)) {
            fractionsDependencies.add(dependency);
        }//from   w w w.  jav  a2s . c om
    }

    Map<String, Fraction> fractions = new HashMap<>();

    for (Dependency dependency : fractionsDependencies) {
        fractions.put(dependency.getGroupId() + ":" + dependency.getArtifactId(),
                new Fraction(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion()));
    }

    for (Dependency dependency : fractionsDependencies) {
        Fraction current = fractions.get(dependency.getGroupId() + ":" + dependency.getArtifactId());
        try {
            MavenProject fractionProject = project(dependency);

            Set<Artifact> deps = fractionProject.getArtifacts();

            for (Artifact each : deps) {
                Fraction f = fractions.get(each.getGroupId() + ":" + each.getArtifactId());
                if (f == null) {
                    continue;
                }
                current.addDependency(f);
            }

        } catch (ProjectBuildingException e) {
            e.printStackTrace();
        }
    }

    File outFile = new File(this.project.getBuild().getOutputDirectory(), "fraction-list.txt");

    outFile.getParentFile().mkdirs();

    try (FileWriter out = new FileWriter(outFile)) {
        for (Fraction each : fractions.values()) {
            out.write(each.toString());
            out.write(" = ");
            out.write(each.getDependenciesString());
            out.write("\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:org.wisdom.maven.osgi.ProjectDependencies.java

License:Apache License

/**
 * Creates the project dependencies instance from a Maven Project.
 *
 * @param project the maven project/*w w  w  .j  ava  2  s .  c  o  m*/
 */
public ProjectDependencies(MavenProject project) {
    this(project.getDependencyArtifacts(), project.getArtifacts());
}