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:de.zalando.mojo.aspectj.AjcHelper.java

License:Open Source License

/**
 * Constructs AspectJ compiler classpath string
 *
 * @param project the Maven Project//from w w  w.j ava  2s  .  c o m
 * @param pluginArtifacts the plugin Artifacts
 * @param outDirs the outputDirectories
 * @return a os spesific classpath string
 */
@SuppressWarnings("unchecked")
public static String createClassPath(MavenProject project, List<Artifact> pluginArtifacts,
        List<String> outDirs) {
    String cp = new String();
    Set<Artifact> classPathElements = Collections.synchronizedSet(new LinkedHashSet<Artifact>());
    Set<Artifact> dependencyArtifacts = project.getDependencyArtifacts();
    classPathElements
            .addAll(dependencyArtifacts == null ? Collections.<Artifact>emptySet() : dependencyArtifacts);
    classPathElements.addAll(project.getArtifacts());
    classPathElements.addAll(pluginArtifacts == null ? Collections.<Artifact>emptySet() : pluginArtifacts);
    for (Artifact classPathElement : classPathElements) {
        File artifact = classPathElement.getFile();
        if (null != artifact) {
            cp += classPathElement.getFile().getAbsolutePath();
            cp += File.pathSeparatorChar;
        }
    }
    Iterator<String> outIter = outDirs.iterator();
    while (outIter.hasNext()) {
        cp += outIter.next();
        cp += File.pathSeparatorChar;
    }

    if (cp.endsWith("" + File.pathSeparatorChar)) {
        cp = cp.substring(0, cp.length() - 1);
    }

    cp = StringUtils.replace(cp, "//", "/");
    return cp;
}

From source file:guru.nidi.maven.tools.backport7to6.AbstractBackport7to6Mojo.java

License:Apache License

private void apply(MavenProject project, ClassFileVisitor v) throws IOException {
    v.process(new File(project.getBuild().getOutputDirectory()));
    for (Artifact artifact : project.getArtifacts()) {
        if (artifact.getArtifactHandler().isAddedToClasspath()) {
            if (Artifact.SCOPE_COMPILE.equals(artifact.getScope())
                    || Artifact.SCOPE_PROVIDED.equals(artifact.getScope())
                    || Artifact.SCOPE_SYSTEM.equals(artifact.getScope())) {
                v.process(artifact.getFile());
            }/* w  w w  .j  av a  2 s.  c  om*/
        }
    }
}

From source file:hudson.gridmaven.reporters.MavenFingerprinter.java

License:Open Source License

/**
 * Mojos perform different dependency resolution, so we need to check this for each mojo.
 *//*from   w  w w .jav  a2 s.c  om*/
public boolean postExecute(MavenBuildProxy build, MavenProject pom, MojoInfo mojo, BuildListener listener,
        Throwable error) throws InterruptedException, IOException {
    // TODO (kutzi, 2011/09/06): it should be perfectly safe to move all these records to the
    // postBuild method as artifacts should only be added by mojos, but never removed/modified.
    record(pom.getArtifacts(), used);
    record(pom.getArtifact(), produced);
    record(pom.getAttachedArtifacts(), produced);
    record(pom.getGroupId() + ":" + pom.getArtifactId(), pom.getFile(), produced);

    return true;
}

From source file:io.fabric8.maven.core.util.MavenUtil.java

License:Apache License

/**
 * Returns true if the maven project has a dependency with the given groupId and artifactId (if not null)
 *//*from  w  w  w.ja  va  2  s  . c  o  m*/
public static boolean hasDependency(MavenProject project, String groupId, String artifactId) {
    Set<Artifact> artifacts = project.getArtifacts();
    if (artifacts != null) {
        for (Artifact artifact : artifacts) {
            String scope = artifact.getScope();
            if (Objects.equal("test", scope)) {
                continue;
            }
            if (artifactId != null && !Objects.equal(artifactId, artifact.getArtifactId())) {
                continue;
            }
            if (Objects.equal(groupId, artifact.getGroupId())) {
                return true;
            }
        }
    }
    return false;
}

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

License:Apache License

/** Replies the current classpath.
 *
 * @return the current classpath.//w  w w  .ja  va2  s .  com
 * @throws MojoExecutionException on failure.
 */
protected List<File> getClassPath() throws MojoExecutionException {
    final Set<String> classPath = new LinkedHashSet<>();
    final MavenProject project = getProject();
    classPath.add(project.getBuild().getSourceDirectory());
    try {
        classPath.addAll(project.getCompileClasspathElements());
    } catch (DependencyResolutionRequiredException e) {
        throw new MojoExecutionException(e.getLocalizedMessage(), e);
    }
    for (final Artifact dep : project.getArtifacts()) {
        classPath.add(dep.getFile().getAbsolutePath());
    }
    classPath.remove(project.getBuild().getOutputDirectory());
    final List<File> files = new ArrayList<>();
    for (final String filename : classPath) {
        final File file = new File(filename);
        if (file.exists()) {
            files.add(file);
        } else {
            getLog().warn(Locale.getString(AbstractSarlBatchCompilerMojo.class, "ERROR_3", filename)); //$NON-NLS-1$
        }
    }
    return files;
}

From source file:io.takari.m2e.jdt.core.internal.JavaConfigurator.java

License:Open Source License

private void configureClasspathAccessRules(IMavenProjectFacade facade, IClasspathDescriptor classpath,
        boolean transitiveDependencyReference, boolean privatePackageReference, IProgressMonitor monitor)
        throws CoreException {

    MavenProject mavenProject = facade.getMavenProject(monitor);

    Map<ArtifactKey, Artifact> dependencies = new HashMap<>();
    Set<ArtifactKey> directDependencies = new HashSet<>();

    for (Artifact artifact : mavenProject.getArtifacts()) {
        dependencies.put(toArtifactKey(artifact), artifact);
    }/*from w w w  .  j a  v  a 2s. co m*/
    for (Dependency artifact : mavenProject.getDependencies()) {
        directDependencies.add(toArtifactKey(artifact));
    }

    for (IClasspathEntryDescriptor entry : classpath.getEntryDescriptors()) {
        ArtifactKey artifactKey = entry.getArtifactKey();
        if (!dependencies.containsKey(artifactKey)) {
            continue; // ignore custom classpath entries
        }
        if (transitiveDependencyReference && !directDependencies.contains(artifactKey)) {
            entry.addAccessRule(JavaCore.newAccessRule(new Path("**"),
                    IAccessRule.K_NON_ACCESSIBLE | IAccessRule.IGNORE_IF_BETTER));
        } else if (privatePackageReference) {
            Collection<String> exportedPackages = getExportedPackages(dependencies.get(artifactKey));
            if (exportedPackages != null) {
                for (String exportedPackage : exportedPackages) {
                    IPath pattern = new Path(exportedPackage).append("/*");
                    entry.addAccessRule(JavaCore.newAccessRule(pattern, IAccessRule.K_ACCESSIBLE));
                }
                entry.addAccessRule(JavaCore.newAccessRule(new Path("**"),
                        IAccessRule.K_NON_ACCESSIBLE | IAccessRule.IGNORE_IF_BETTER));
            }
        }
    }
}

From source file:io.treefarm.plugins.haxe.components.HaxeCompiler.java

License:Apache License

private void addLibs(List<String> argumentsList, MavenProject project) {
    for (Artifact artifact : project.getArtifacts()) {
        if (artifact.getType().equals(HaxeFileExtensions.HAXELIB)
                || artifact.getType().equals(HaxeFileExtensions.POM_HAXELIB)) {
            String haxelibId = artifact.getArtifactId() + ":" + artifact.getVersion();
            argumentsList.add("-lib");
            argumentsList.add(haxelibId);
        }// w  w  w  .  ja va  2 s .  c  o m
    }
}

From source file:io.treefarm.plugins.haxe.components.HaxeCompiler.java

License:Apache License

private void addHars(List<String> argumentsList, MavenProject project, Set<CompileTarget> targets) {
    File dependenciesDirectory = new File(outputDirectory, "dependencies");

    if (!dependenciesDirectory.exists())
        dependenciesDirectory.mkdir();/*from   w w w .j  av  a2 s.com*/

    for (Artifact artifact : project.getArtifacts()) {
        if (artifact.getType().equals(HaxeFileExtensions.HAR)) {
            File harUnpackDirectory = new File(dependenciesDirectory, artifact.getArtifactId());

            if (!harUnpackDirectory.exists()) {
                harUnpackDirectory.mkdir();
                ZipUnArchiver unArchiver = new ZipUnArchiver();
                unArchiver.enableLogging(logger);
                unArchiver.setSourceFile(artifact.getFile());
                unArchiver.setDestDirectory(harUnpackDirectory);
                unArchiver.extract();
            }

            try {
                File metadataFile = new File(harUnpackDirectory, HarMetadata.METADATA_FILE_NAME);
                JAXBContext jaxbContext = JAXBContext.newInstance(HarMetadata.class, CompileTarget.class);
                HarMetadata metadata = (HarMetadata) jaxbContext.createUnmarshaller().unmarshal(metadataFile);

                if (!metadata.target.containsAll(targets))
                    logger.warn("Dependency " + artifact + " is not compatible with your compile targets.");
            } catch (JAXBException e) {
                logger.warn("Can't read " + artifact + "metadata", e);
            }

            addSourcePath(argumentsList, harUnpackDirectory.getAbsolutePath());
        }
    }
}

From source file:ms.dew.devops.maven.function.AppKindPluginSelector.java

License:Apache License

/**
 * Select app kind plugin./*from   w ww. j  a v  a 2 s .c  o  m*/
 *
 * @param mavenProject the maven project
 * @return the optional
 */
public static Optional<AppKindPlugin> select(MavenProject mavenProject) {
    if (mavenProject.getPackaging().equalsIgnoreCase("maven-plugin")) {
        return Optional.empty();
    }
    if (new File(mavenProject.getBasedir().getPath() + File.separator + "package.json").exists()) {
        return Optional.of(new FrontendNodeAppKindPlugin());
    }
    if (mavenProject.getPackaging().equalsIgnoreCase("jar")
            && new File(mavenProject.getBasedir().getPath() + File.separator + "src" + File.separator + "main"
                    + File.separator + "resources").exists()
            && Arrays
                    .stream(new File(mavenProject.getBasedir().getPath() + File.separator + "src"
                            + File.separator + "main" + File.separator + "resources").listFiles())
                    .anyMatch((res -> res.getName().toLowerCase().contains("application")
                            || res.getName().toLowerCase().contains("bootstrap")))
            && mavenProject.getArtifacts().stream()
                    .map(artifact -> artifact.getGroupId() + ":" + artifact.getArtifactId())
                    .anyMatch("org.springframework.boot:spring-boot-starter-web"::equalsIgnoreCase)) {
        return Optional.of(new JvmServiceSpringBootAppKindPlugin());
    }
    if (mavenProject.getPackaging().equalsIgnoreCase("jar")) {
        return Optional.of(new JvmLibAppKindPlugin());
    }
    if (mavenProject.getPackaging().equalsIgnoreCase("pom")) {
        return Optional.of(new PomAppKindPlugin());
    }
    return Optional.empty();
}

From source file:net.dynamic_tools.maven.plugin.javascript.util.JavaScriptArtifactManager.java

License:Apache License

/**
 * if you are using this method make sure the mojo has @requiresDependencyResolution [scope]
 *//* w  ww .  j a  v a2 s . c  o m*/
public void unpack(MavenProject project, String scope, File target, boolean useArtifactId)
        throws ArchiverException {
    archiver.setOverwrite(false);

    Set dependencies = project.getArtifacts();

    if (dependencies.size() == 0) {
        getLogger().warn(
                "JavaScriptArtifactManager.unpack: # of dependencies = 0... make sure the calling mojo is using @requiresDependencyResolution. this may not be an error if your project has no dependencies.");
    }

    ArtifactFilter runtime = new ScopeArtifactFilter(scope);
    for (Iterator iterator = dependencies.iterator(); iterator.hasNext();) {
        Artifact dependency = (Artifact) iterator.next();

        if (!dependency.isOptional() && Types.JAVASCRIPT_TYPE.equals(dependency.getType())
                && runtime.include(dependency)) {
            getLogger().info("Unpack javascript dependency [" + dependency.toString() + "]");
            archiver.setSourceFile(dependency.getFile());

            unpack(dependency, target, useArtifactId);
        }
    }
}