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.jfrog.build.extractor.maven.BuildInfoRecorder.java

License:Apache License

private void extractModuleDependencies(MavenProject project) {
    Set<Artifact> moduleDependencies = currentModuleDependencies.get();
    if (moduleDependencies == null) {
        logger.warn(/*  w  ww. java 2 s.  co  m*/
                "Skipping Artifactory Build-Info project dependency extraction: Null current module dependencies.");
        return;
    }

    mergeProjectDependencies(project.getArtifacts());
}

From source file:org.jfrog.hudson.maven2.MavenDependenciesRecorder.java

License:Apache License

/**
 * Mojos perform different dependency resolution, so we add dependencies for each mojo.
 *//*from   w  w  w  .j ava  2 s  .  com*/
@Override
public boolean postExecute(MavenBuildProxy build, MavenProject pom, MojoInfo mojo, BuildListener listener,
        Throwable error) {
    //listener.getLogger().println("[MavenDependenciesRecorder] mojo: " + mojo.getClass() + ":" + mojo.getGoal());
    //listener.getLogger().println("[MavenDependenciesRecorder] dependencies: " + pom.getArtifacts());
    recordMavenDependencies(pom.getArtifacts());
    return true;
}

From source file:org.jszip.maven.RunMojo.java

License:Apache License

private void addCssEngineResources(MavenProject project, List<MavenProject> reactorProjects, Mapping[] mappings,
        List<Resource> _resources) throws MojoExecutionException, IOException {
    List<PseudoFileSystem.Layer> layers = new ArrayList<PseudoFileSystem.Layer>();
    layers.add(new PseudoFileSystem.FileLayer("/virtual", warSourceDirectory));
    FilterArtifacts filter = new FilterArtifacts();

    filter.addFilter(new ProjectTransitivityFilter(project.getDependencyArtifacts(), false));

    filter.addFilter(new ScopeFilter("runtime", ""));

    filter.addFilter(new TypeFilter(JSZIP_TYPE, ""));

    // start with all artifacts.
    Set<Artifact> artifacts = project.getArtifacts();

    // perform filtering
    try {/*from   w w  w  .j ava 2  s  .c  o  m*/
        artifacts = filter.filter(artifacts);
    } catch (ArtifactFilterException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }

    for (Artifact artifact : artifacts) {
        String path = Mapping.getArtifactPath(mappings, artifact);
        getLog().info("Adding " + ArtifactUtils.key(artifact) + " to virtual filesystem");
        File file = artifact.getFile();
        if (file.isDirectory()) {
            MavenProject fromReactor = findProject(reactorProjects, artifact);
            if (fromReactor != null) {
                MavenSession session = this.session.clone();
                session.setCurrentProject(fromReactor);
                Plugin plugin = findThisPluginInProject(fromReactor);
                try {
                    // we cheat here and use our version of the plugin... but this is less of a cheat than the only
                    // other way which is via reflection.
                    MojoDescriptor jszipDescriptor = findMojoDescriptor(this.pluginDescriptor, JSZipMojo.class);

                    for (PluginExecution pluginExecution : plugin.getExecutions()) {
                        if (!pluginExecution.getGoals().contains(jszipDescriptor.getGoal())) {
                            continue;
                        }
                        MojoExecution mojoExecution = createMojoExecution(plugin, pluginExecution,
                                jszipDescriptor);
                        JSZipMojo mojo = (JSZipMojo) mavenPluginManager
                                .getConfiguredMojo(org.apache.maven.plugin.Mojo.class, session, mojoExecution);
                        try {
                            File contentDirectory = mojo.getContentDirectory();
                            if (contentDirectory.isDirectory()) {
                                getLog().debug("Merging directory " + contentDirectory + " into " + path);
                                layers.add(new PseudoFileSystem.FileLayer(path, contentDirectory));
                            }
                            File resourcesDirectory = mojo.getResourcesDirectory();
                            if (resourcesDirectory.isDirectory()) {
                                getLog().debug("Merging directory " + contentDirectory + " into " + path);
                                layers.add(new PseudoFileSystem.FileLayer(path, resourcesDirectory));
                            }
                        } finally {
                            mavenPluginManager.releaseMojo(mojo, mojoExecution);
                        }
                    }
                } catch (PluginConfigurationException e) {
                    throw new MojoExecutionException(e.getMessage(), e);
                } catch (PluginContainerException e) {
                    throw new MojoExecutionException(e.getMessage(), e);
                }
            } else {
                throw new MojoExecutionException("Cannot find jzsip artifact: " + artifact.getId());
            }
        } else {
            try {
                getLog().debug("Merging .zip file " + file + " into " + path);
                layers.add(new PseudoFileSystem.ZipLayer(path, file));
            } catch (IOException e) {
                throw new MojoExecutionException(e.getMessage(), e);
            }
        }
    }

    final PseudoFileSystem fs = new PseudoFileSystem(layers);

    CssEngine engine = new LessEngine(fs, encoding == null ? "utf-8" : encoding, getLog(), lessCompress,
            customLessScript, showErrorExtracts);

    // look for files to compile

    PseudoDirectoryScanner scanner = new PseudoDirectoryScanner();

    scanner.setFileSystem(fs);

    scanner.setBasedir(fs.getPseudoFile("/virtual"));

    if (lessIncludes != null && !lessIncludes.isEmpty()) {
        scanner.setIncludes(processIncludesExcludes(lessIncludes));
    } else {
        scanner.setIncludes(new String[] { "**/*.less" });
    }

    if (lessExcludes != null && !lessExcludes.isEmpty()) {
        scanner.setExcludes(processIncludesExcludes(lessExcludes));
    } else {
        scanner.setExcludes(new String[0]);
    }

    scanner.scan();

    for (String fileName : new ArrayList<String>(Arrays.asList(scanner.getIncludedFiles()))) {
        final CssEngineResource child = new CssEngineResource(fs, engine, "/virtual/" + fileName,
                new File(webappDirectory, engine.mapName(fileName)));
        final String path = FileUtils.dirname(fileName);
        if (StringUtils.isBlank(path)) {
            _resources.add(
                    new VirtualDirectoryResource(new VirtualDirectoryResource(child, child.getName()), ""));
        } else {
            _resources.add(
                    new VirtualDirectoryResource(new VirtualDirectoryResource(child, child.getName()), path));
        }
    }

    engine = new SassEngine(fs, encoding == null ? "utf-8" : encoding);

    if (sassIncludes != null && !sassIncludes.isEmpty()) {
        scanner.setIncludes(processIncludesExcludes(sassIncludes));
    } else {
        scanner.setIncludes(new String[] { "**/*.sass", "**/*.scss" });
    }

    if (sassExcludes != null && !sassExcludes.isEmpty()) {
        scanner.setExcludes(processIncludesExcludes(sassExcludes));
    } else {
        scanner.setExcludes(new String[] { "**/_*.sass", "**/_*.scss" });
    }

    scanner.scan();

    for (String fileName : new ArrayList<String>(Arrays.asList(scanner.getIncludedFiles()))) {
        final CssEngineResource child = new CssEngineResource(fs, engine, "/virtual/" + fileName,
                new File(webappDirectory, engine.mapName(fileName)));
        final String path = FileUtils.dirname(fileName);
        if (StringUtils.isBlank(path)) {
            _resources.add(
                    new VirtualDirectoryResource(new VirtualDirectoryResource(child, child.getName()), ""));
        } else {
            _resources.add(
                    new VirtualDirectoryResource(new VirtualDirectoryResource(child, child.getName()), path));
        }
    }

}

From source file:org.jszip.maven.RunMojo.java

License:Apache License

@SuppressWarnings("unchecked")
private Set<Artifact> getOverlayArtifacts(MavenProject project, String scope) throws ArtifactFilterException {
    FilterArtifacts filter = new FilterArtifacts();

    filter.addFilter(new ProjectTransitivityFilter(project.getDependencyArtifacts(), false));

    filter.addFilter(new ScopeFilter(scope, ""));

    filter.addFilter(new TypeFilter(JSZIP_TYPE, ""));

    return filter.filter(project.getArtifacts());
}

From source file:org.jvnet.maven.plugin.antrun.AbstractAntMojo.java

License:Apache License

/**
 * @param antTasks/*from   w  w w . j a  v a 2s .  c om*/
 * @param mavenProject
 * @throws MojoExecutionException
 */
protected void executeTasks(Target antTasks, MavenProject mavenProject, List pluginArtifacts)
        throws MojoExecutionException {
    if (antTasks == null) {
        getLog().info("No ant tasks defined - SKIPPED");
        return;
    }

    try {
        //TODO refactor - place the manipulation of the expressionEvaluator into a separated class.
        ExpressionEvaluator exprEvaluator = (ExpressionEvaluator) antTasks.getProject()
                .getReference(AntTargetConverter.MAVEN_EXPRESSION_EVALUATOR_ID);

        Project antProject = antTasks.getProject();

        PropertyHelper propertyHelper = PropertyHelper.getPropertyHelper(antProject);
        propertyHelper.setNext(new AntPropertyHelper(exprEvaluator, mavenProject.getArtifacts(), getLog()));

        DefaultLogger antLogger = new DefaultLogger();
        antLogger.setOutputPrintStream(System.out);
        antLogger.setErrorPrintStream(System.err);
        antLogger.setMessageOutputLevel(getLog().isDebugEnabled() ? Project.MSG_DEBUG : Project.MSG_INFO);

        antProject.addBuildListener(antLogger);
        antProject.setBaseDir(mavenProject.getBasedir());

        Path p = new Path(antProject);
        p.setPath(StringUtils.join(mavenProject.getCompileClasspathElements().iterator(), File.pathSeparator));

        /* maven.dependency.classpath it's deprecated as it's equal to maven.compile.classpath */
        antProject.addReference("maven.dependency.classpath", p);
        antProject.addReference("maven.compile.classpath", p);

        p = new Path(antProject);
        p.setPath(StringUtils.join(mavenProject.getRuntimeClasspathElements().iterator(), File.pathSeparator));
        antProject.addReference("maven.runtime.classpath", p);

        p = new Path(antProject);
        p.setPath(StringUtils.join(mavenProject.getTestClasspathElements().iterator(), File.pathSeparator));
        antProject.addReference("maven.test.classpath", p);

        /* set maven.plugin.classpath with plugin dependencies */
        antProject.addReference("maven.plugin.classpath", getPathFromArtifacts(pluginArtifacts, antProject));

        if (getLog().isInfoEnabled()) {
            getLog().info("Executing tasks");
        }

        configureProject(antProject);

        antTasks.execute();

        if (getLog().isInfoEnabled()) {
            getLog().info("Executed tasks");
        }
    } catch (DependencyResolutionRequiredException e) {
        throw new MojoExecutionException("DependencyResolutionRequiredException: " + e.getMessage(), e);
    } catch (BuildException e) {
        throw new MojoExecutionException("An Ant BuildException has occured: " + e.getMessage(), e);
    } catch (Exception e) {
        throw new MojoExecutionException("Error executing ant tasks: " + e.getMessage(), e);
    }
}

From source file:org.kaazing.license.maven.plugin.AbstractLicenseMojo.java

License:Open Source License

Set<Artifact> getDependencyArtifacts(MavenProject project) {
    @SuppressWarnings("unchecked")
    Set<Artifact> artifacts = project.getArtifacts();
    return artifacts;
}

From source file:org.keedio.maven.plugins.shade.filter.MinijarFilter.java

License:Apache License

/**
 * @since 1.6/*  w w  w . jav a2  s.co m*/
 */
@SuppressWarnings({ "unchecked" })
public MinijarFilter(MavenProject project, Log log, List<SimpleFilter> simpleFilters) throws IOException {

    this.log = log;

    Clazzpath cp = new Clazzpath();

    ClazzpathUnit artifactUnit = cp.addClazzpathUnit(new FileInputStream(project.getArtifact().getFile()),
            project.toString());

    for (Artifact dependency : project.getArtifacts()) {
        addDependencyToClasspath(cp, dependency);
    }

    removable = cp.getClazzes();
    removePackages(artifactUnit);
    removable.removeAll(artifactUnit.getClazzes());
    removable.removeAll(artifactUnit.getTransitiveDependencies());
    removeSpecificallyIncludedClasses(project,
            simpleFilters == null ? Collections.<SimpleFilter>emptyList() : simpleFilters);
}

From source file:org.keedio.maven.plugins.shade.filter.MinijarFilter.java

License:Apache License

private void removeSpecificallyIncludedClasses(MavenProject project, List<SimpleFilter> simpleFilters)
        throws IOException {
    //remove classes specifically included in filters
    Clazzpath checkCp = new Clazzpath();
    for (Artifact dependency : project.getArtifacts()) {
        File jar = dependency.getFile();

        for (SimpleFilter simpleFilter : simpleFilters) {
            if (simpleFilter.canFilter(jar)) {
                ClazzpathUnit depClazzpathUnit = addDependencyToClasspath(checkCp, dependency);
                if (depClazzpathUnit != null) {
                    Iterator<Clazz> j = removable.iterator();
                    while (j.hasNext()) {
                        Clazz clazz = j.next();

                        if (depClazzpathUnit.getClazzes().contains(clazz) //
                                && simpleFilter.isSpecificallyIncluded(clazz.getName().replace('.', '/'))) {
                            log.info(clazz.getName() + " not removed because it was specifically included");
                            j.remove();//from   www .j  av  a2s  . co m
                        }
                    }
                }
            }
        }
    }
}

From source file:org.kie.maven.plugin.InjectReactiveMojo.java

License:Apache License

private List<URL> dependenciesURLs() throws MojoExecutionException {
    List<URL> urls = new ArrayList<>();
    // HHH-10145 Add dependencies to classpath as well - all but the ones used for testing purposes
    Set<Artifact> artifacts = null;
    MavenProject project = ((MavenProject) getPluginContext().get("project"));
    if (project != null) {
        // Prefer execution project when available (it includes transient dependencies)
        MavenProject executionProject = project.getExecutionProject();
        artifacts = (executionProject != null ? executionProject.getArtifacts() : project.getArtifacts());
    }/* w w w.ja  v  a2  s.c o  m*/
    if (artifacts != null) {
        for (Artifact a : artifacts) {
            if (!Artifact.SCOPE_TEST.equals(a.getScope())) {
                try {
                    urls.add(a.getFile().toURI().toURL());
                    getLog().debug("Adding classpath entry for dependency " + a.getId());
                } catch (MalformedURLException e) {
                    String msg = "Unable to resolve URL for dependency " + a.getId() + " at "
                            + a.getFile().getAbsolutePath();
                    if (failOnError) {
                        throw new MojoExecutionException(msg, e);
                    }
                    getLog().warn(msg);
                }
            }
        }
    }
    return urls;
}

From source file:org.linuxstuff.mojo.licensing.DefaultDependenciesTool.java

License:Open Source License

/**
 * {@inheritDoc}/*from w  w  w .ja  va2  s .  c o m*/
 */
@Override
public SortedMap<String, MavenProject> loadProjectDependencies(MavenProject project,
        MavenProjectDependenciesConfigurator configuration, ArtifactRepository localRepository,
        List<ArtifactRepository> remoteRepositories, SortedMap<String, MavenProject> cache) {

    boolean haveNoIncludedGroups = StringUtils.isEmpty(configuration.getIncludedGroups());
    boolean haveNoIncludedArtifacts = StringUtils.isEmpty(configuration.getIncludedArtifacts());

    boolean haveExcludedGroups = StringUtils.isNotEmpty(configuration.getExcludedGroups());
    boolean haveExcludedArtifacts = StringUtils.isNotEmpty(configuration.getExcludedArtifacts());
    boolean haveExclusions = haveExcludedGroups || haveExcludedArtifacts;

    Pattern includedGroupPattern = null;
    Pattern includedArtifactPattern = null;
    Pattern excludedGroupPattern = null;
    Pattern excludedArtifactPattern = null;

    if (!haveNoIncludedGroups) {
        includedGroupPattern = Pattern.compile(configuration.getIncludedGroups());
    }
    if (!haveNoIncludedArtifacts) {
        includedArtifactPattern = Pattern.compile(configuration.getIncludedArtifacts());
    }
    if (haveExcludedGroups) {
        excludedGroupPattern = Pattern.compile(configuration.getExcludedGroups());
    }
    if (haveExcludedArtifacts) {
        excludedArtifactPattern = Pattern.compile(configuration.getExcludedArtifacts());
    }

    Set<?> depArtifacts;

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

    List<String> includedScopes = configuration.getIncludedScopes();
    List<String> excludeScopes = configuration.getExcludedScopes();

    SortedMap<String, MavenProject> result = new TreeMap<String, MavenProject>();

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

        String scope = artifact.getScope();
        if (!includedScopes.isEmpty() && !includedScopes.contains(scope)) {

            // not in included scopes
            continue;
        }
        {
            if (excludeScopes.contains(scope)) {

                // in excluded scopes
                continue;
            }
        }

        Logger log = getLogger();

        String id = artifact.getId();

        log.debug("detected artifact " + id);

        // Check if the project should be included
        // If there is no specified artifacts and group to include, include
        // all
        boolean isToInclude = haveNoIncludedArtifacts && haveNoIncludedGroups
                || isIncludable(artifact, includedGroupPattern, includedArtifactPattern);

        // Check if the project should be excluded
        boolean isToExclude = isToInclude && haveExclusions
                && isExcludable(artifact, excludedGroupPattern, excludedArtifactPattern);

        if (!isToInclude || isToExclude) {
            log.debug("skip artifact " + id);
            continue;
        }

        MavenProject depMavenProject = null;

        if (cache != null) {

            // try to get project from cache
            depMavenProject = cache.get(id);
        }

        if (depMavenProject != null) {
            log.debug("add dependency [" + id + "] (from cache)");
        } else {

            // build project

            try {
                depMavenProject = mavenProjectBuilder.buildFromRepository(artifact, remoteRepositories,
                        localRepository, true);
            } catch (ProjectBuildingException e) {
                log.warn("Unable to obtain POM for artifact : " + artifact, e);
                continue;
            }

            log.debug("add dependency [" + id + "]");
            if (cache != null) {

                // store it also in cache
                cache.put(id, depMavenProject);
            }
        }

        // keep the project
        result.put(id, depMavenProject);
    }

    return result;
}