Java Code Examples for org.apache.maven.artifact.Artifact#getScope()

The following examples show how to use org.apache.maven.artifact.Artifact#getScope() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MavenUtil.java    From jkube with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Returns the version associated to the dependency with the given groupId and artifactId (if present)
 *
 * @param project MavenProject object for project
 * @param groupId group id
 * @param artifactId artifact id
 * @return version associated to dependency
 */
public static String getDependencyVersion(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 artifact.getVersion();
            }
        }
    }
    return null;
}
 
Example 2
Source File: Mojo.java    From capsule-maven-plugin with MIT License 6 votes vote down vote up
Artifact toArtifact(final ArtifactResult ar) {
	if (ar == null) return null;
	final Artifact artifact = new org.apache.maven.artifact.DefaultArtifact(
			ar.getArtifact().getGroupId(),
			ar.getArtifact().getArtifactId(),
			ar.getArtifact().getVersion(),
			null,
			"jar",
			ar.getArtifact().getClassifier(),
			null);
	if (ar.getRequest().getDependencyNode() != null && ar.getRequest().getDependencyNode().getDependency() != null) {
		artifact.setScope(ar.getRequest().getDependencyNode().getDependency().getScope());
		artifact.setOptional(ar.getRequest().getDependencyNode().getDependency().isOptional());
	}
	if (artifact.getScope() == null || artifact.getScope().isEmpty()) artifact.setScope("compile");
	artifact.setFile(ar.getArtifact().getFile());
	return artifact;
}
 
Example 3
Source File: Mojo.java    From capsule-maven-plugin with MIT License 6 votes vote down vote up
private Set<Artifact> getDependencyArtifactsOf(final Dependency dependency, final boolean includeRoot) {
	final Set<Artifact> artifacts = new HashSet<>();
	if (includeRoot) artifacts.add(toArtifact(dependency));
	for (final ArtifactResult ar : resolveDependencies(dependency)) {
		final Artifact artifact = toArtifact(ar);

		// if null set to default compile
		if (artifact.getScope() == null || artifact.getScope().isEmpty()) artifact.setScope("compile");

		// skip any deps that aren't compile or runtime
		if (!artifact.getScope().equals("compile") && !artifact.getScope().equals("runtime")) continue;

		// set direct-scope on transitive deps
		if (dependency.getScope().equals("provided")) artifact.setScope("provided");
		if (dependency.getScope().equals("system")) artifact.setScope("system");
		if (dependency.getScope().equals("test")) artifact.setScope("test");

		artifacts.add(toArtifact(ar));
	}
	return cleanArtifacts(artifacts);
}
 
Example 4
Source File: UseLatestReleasesMojo.java    From versions-maven-plugin with Apache License 2.0 6 votes vote down vote up
private ArtifactVersion[] filterVersionsWithIncludes( ArtifactVersion[] newer, Artifact artifact )
{
    List<ArtifactVersion> filteredNewer = new ArrayList<>( newer.length );
    for ( int j = 0; j < newer.length; j++ )
    {
        ArtifactVersion artifactVersion = newer[j];
        Artifact artefactWithNewVersion =
            new DefaultArtifact( artifact.getGroupId(), artifact.getArtifactId(),
                                 VersionRange.createFromVersion( artifactVersion.toString() ), artifact.getScope(),
                                 artifact.getType(), null, new DefaultArtifactHandler(), false );
        if ( isIncluded( artefactWithNewVersion ) )
        {
            filteredNewer.add( artifactVersion );
        }
    }
    return filteredNewer.toArray( new ArtifactVersion[filteredNewer.size()] );
}
 
Example 5
Source File: ReportingResolutionListener.java    From maven-confluence-plugin with Apache License 2.0 6 votes vote down vote up
@Override
public void manageArtifact( Artifact artifact, Artifact replacement )
{
    Node node = artifacts.get( artifact.getDependencyConflictId() );

    if ( node != null )
    {
        if ( replacement.getVersion() != null )
        {
            node.artifact.setVersion( replacement.getVersion() );
        }
        if ( replacement.getScope() != null )
        {
            node.artifact.setScope( replacement.getScope() );
        }
    }
}
 
Example 6
Source File: BaseCycloneDxMojo.java    From cyclonedx-maven-plugin with Apache License 2.0 6 votes vote down vote up
protected boolean shouldInclude(Artifact artifact) {
    if (artifact.getScope() == null) {
        return false;
    }
    if (excludeTypes != null) {
        final boolean shouldExclude = Arrays.stream(excludeTypes).anyMatch(artifact.getType()::equals);
        if (shouldExclude) {
            return false;
        }
    }
    if (includeCompileScope && "compile".equals(artifact.getScope())) {
        return true;
    } else if (includeProvidedScope && "provided".equals(artifact.getScope())) {
        return true;
    } else if (includeRuntimeScope && "runtime".equals(artifact.getScope())) {
        return true;
    } else if (includeTestScope && "test".equals(artifact.getScope())) {
        return true;
    } else if (includeSystemScope && "system".equals(artifact.getScope())) {
        return true;
    }
    return false;
}
 
Example 7
Source File: SearchClassDependencyInRepo.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private Artifact getArtifact(NbMavenProject mavProj, List<NBVersionInfo> nbvis, boolean isTestSource) {
    MavenProject mp = mavProj.getMavenProject();
    List<Artifact> arts = new LinkedList<Artifact>(isTestSource ? mp.getTestArtifacts() : mp.getCompileArtifacts());
    for (NBVersionInfo info : nbvis) {
        for (Artifact a : arts) {
            if (a.getGroupId() != null && a.getGroupId().equals(info.getGroupId())) {
                if (a.getArtifactId() != null && a.getArtifactId().equals(info.getArtifactId())) {
                    String scope = a.getScope();
                    if ("compile".equals(scope) || "test".equals(scope)) { // NOI18N
                        return a;
                    }
                }
            }
        }
    }
    return null;
}
 
Example 8
Source File: AbstractSwarmMojo.java    From thorntail with Apache License 2.0 5 votes vote down vote up
protected ArtifactSpec artifactToArtifactSpec(Artifact dep) {
    return new ArtifactSpec(dep.getScope(),
                            dep.getGroupId(),
                            dep.getArtifactId(),
                            dep.getBaseVersion(),
                            dep.getType(),
                            dep.getClassifier(),
                            dep.getFile());
}
 
Example 9
Source File: AbstractSwarmMojo.java    From thorntail with Apache License 2.0 5 votes vote down vote up
private static ArtifactSpec asBucketKey(Artifact artifact) {

        return new ArtifactSpec(
                artifact.getScope(),
                artifact.getGroupId(),
                artifact.getArtifactId(),
                artifact.getBaseVersion(),
                artifact.getType(),
                artifact.getClassifier(),
                artifact.getFile()
        );
    }
 
Example 10
Source File: AbstractSwarmMojo.java    From wildfly-swarm with Apache License 2.0 5 votes vote down vote up
protected ArtifactSpec artifactToArtifactSpec(Artifact dep) {
    return new ArtifactSpec(dep.getScope(),
                            dep.getGroupId(),
                            dep.getArtifactId(),
                            dep.getBaseVersion(),
                            dep.getType(),
                            dep.getClassifier(),
                            dep.getFile());
}
 
Example 11
Source File: ExampleGraphMojo.java    From depgraph-maven-plugin with Apache License 2.0 5 votes vote down vote up
private static DependencyNode createConflict(Artifact artifact, String winningVersion) {
  org.eclipse.aether.artifact.DefaultArtifact aetherArtifact = new org.eclipse.aether.artifact.DefaultArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getClassifier(), artifact.getType(), artifact.getVersion());
  org.eclipse.aether.artifact.DefaultArtifact winnerArtifact = new org.eclipse.aether.artifact.DefaultArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getClassifier(), artifact.getType(), winningVersion);

  DefaultDependencyNode dependencyNode = new DefaultDependencyNode(new Dependency(aetherArtifact, artifact.getScope()));
  dependencyNode.setData(NODE_DATA_WINNER, new DefaultDependencyNode(new Dependency(winnerArtifact, "compile")));

  return new DependencyNode(dependencyNode);
}
 
Example 12
Source File: Mojo.java    From capsule-maven-plugin with MIT License 5 votes vote down vote up
private Artifact toArtifact(final Dependency dependency) {
	if (dependency == null) return null;
	final Artifact artifact = toArtifact(resolve(dependency));
	artifact.setScope(dependency.getScope());
	if (artifact.getScope() == null || artifact.getScope().isEmpty()) artifact.setScope("compile");
	return artifact;
}
 
Example 13
Source File: DependencyNode.java    From depgraph-maven-plugin with Apache License 2.0 5 votes vote down vote up
private static NodeResolution determineNodeResolution(Artifact artifact) {
  if (artifact.getScope() == null) {
    return NodeResolution.PARENT;
  }

  return NodeResolution.INCLUDED;
}
 
Example 14
Source File: JApiCmpMojo.java    From japicmp with Apache License 2.0 5 votes vote down vote up
private void setUpClassPathUsingMavenProject(JarArchiveComparatorOptions comparatorOptions, MavenParameters mavenParameters, PluginParameters pluginParameters, ConfigurationVersion configurationVersion) throws MojoFailureException {
	notNull(mavenParameters.getMavenProject(), "Maven parameter mavenProject should be provided by maven container.");
	Set<Artifact> dependencyArtifacts = mavenParameters.getMavenProject().getArtifacts();
	Set<String> classPathEntries = new HashSet<>();
	for (Artifact artifact : dependencyArtifacts) {
		String scope = artifact.getScope();
		if (!"test".equals(scope)) {
			Set<Artifact> artifacts = resolveArtifact(artifact, mavenParameters, false, pluginParameters, configurationVersion);
			for (Artifact resolvedArtifact : artifacts) {
				File resolvedFile = resolvedArtifact.getFile();
				if (resolvedFile != null) {
					String absolutePath = resolvedFile.getAbsolutePath();
					if (!classPathEntries.contains(absolutePath)) {
						if (getLog().isDebugEnabled()) {
							getLog().debug("Adding to classpath: " + absolutePath + "; scope: " + scope);
						}
						classPathEntries.add(absolutePath);
					}
				} else {
					handleMissingArtifactFile(pluginParameters, artifact);
				}
			}
		}
	}
	for (String classPathEntry : classPathEntries) {
		comparatorOptions.getClassPathEntries().add(classPathEntry);
	}
}
 
Example 15
Source File: CapsuleMojo.java    From capsule-maven-plugin with MIT License 5 votes vote down vote up
private void addDependencies(final JarOutputStream jar) throws IOException {

		// go through dependencies
		final Set<Artifact> artifacts = includeTransitiveDep ? includedDependencyArtifacts() : includedDirectDependencyArtifacts();

		for (final Artifact artifact : artifacts) {

			final String scope = artifact.getScope() == null || artifact.getScope().isEmpty() ? "compile" : artifact.getScope();

			boolean optionalMatch = true;
			if (artifact.isOptional()) optionalMatch = includeOptionalDep;

			// check artifact has a file
			if (artifact.getFile() == null)
				warn("\t[Dependency] " + coords(artifact) + "(" + artifact.getScope() + ") file not found, thus will not be added to capsule jar.");

			// ignore capsule jar
			if (artifact.getGroupId().equalsIgnoreCase(CAPSULE_GROUP) && artifact.getArtifactId().equalsIgnoreCase(DEFAULT_CAPSULE_NAME))
				continue;

			// check against requested scopes
			if (
					(includeCompileDep && scope.equals("compile") && optionalMatch) ||
							(includeRuntimeDep && scope.equals("runtime") && optionalMatch) ||
							(includeProvidedDep && scope.equals("provided") && optionalMatch) ||
							(includeSystemDep && scope.equals("system") && optionalMatch) ||
							(includeTestDep && scope.equals("test") && optionalMatch)
					) {
				addToJar(artifact.getFile().getName(), new FileInputStream(artifact.getFile()), jar);
				info("\t[Embedded-Dependency] " + coords(artifact) + "(" + scope + ")");
			} else
				debug("\t[Dependency] " + coords(artifact) + "(" + artifact.getScope() + ") skipped, as it does not match any required scope");
		}
	}
 
Example 16
Source File: NativeLinkMojo.java    From maven-native with MIT License 4 votes vote down vote up
/**
 *
 */
private void attachPrimaryArtifact()
{
    Artifact artifact = this.project.getArtifact();

    if ( null == this.classifier )
    {
        artifact.setFile( new File( this.linkerOutputDirectory + "/" + this.project.getBuild().getFinalName() + "."
                + this.project.getArtifact().getArtifactHandler().getExtension() ) );
    }
    else
    {
        // install primary artifact as a classifier

        DefaultArtifact clone = new DefaultArtifact( artifact.getGroupId(), artifact.getArtifactId(),
                artifact.getVersionRange().cloneOf(), artifact.getScope(), artifact.getType(), classifier,
                artifact.getArtifactHandler(), artifact.isOptional() );

        clone.setRelease( artifact.isRelease() );
        clone.setResolvedVersion( artifact.getVersion() );
        clone.setResolved( artifact.isResolved() );
        clone.setFile( artifact.getFile() );

        if ( artifact.getAvailableVersions() != null )
        {
            clone.setAvailableVersions( new ArrayList<>( artifact.getAvailableVersions() ) );
        }

        clone.setBaseVersion( artifact.getBaseVersion() );
        clone.setDependencyFilter( artifact.getDependencyFilter() );

        if ( artifact.getDependencyTrail() != null )
        {
            clone.setDependencyTrail( new ArrayList<>( artifact.getDependencyTrail() ) );
        }

        clone.setDownloadUrl( artifact.getDownloadUrl() );
        clone.setRepository( artifact.getRepository() );

        clone.setFile( new File( this.linkerOutputDirectory + "/" + this.project.getBuild().getFinalName() + "."
                + this.project.getArtifact().getArtifactHandler().getExtension() ) );

        project.setArtifact( clone );
    }
}
 
Example 17
Source File: PackageMojo.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 4 votes vote down vote up
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    initProperties(false);

    final BuildTool tool = new BuildTool();

    tool.projectArtifact(
            this.project.getArtifact().getGroupId(),
            this.project.getArtifact().getArtifactId(),
            this.project.getArtifact().getBaseVersion(),
            this.project.getArtifact().getType(),
            this.project.getArtifact().getFile());


    this.project.getArtifacts()
            .forEach(dep -> tool.dependency(dep.getScope(),
                    dep.getGroupId(),
                    dep.getArtifactId(),
                    dep.getBaseVersion(),
                    dep.getType(),
                    dep.getClassifier(),
                    dep.getFile(),
                    dep.getDependencyTrail().size() == 2));

    List<Resource> resources = this.project.getResources();
    for (Resource each : resources) {
        tool.resourceDirectory(each.getDirectory());
    }

    for (String additionalModule : additionalModules) {
        File source = new File(this.project.getBuild().getOutputDirectory(), additionalModule);
        if (source.exists()) {
            tool.additionalModule(source.getAbsolutePath());
        }
    }

    tool
            .properties(this.properties)
            .mainClass(this.mainClass)
            .bundleDependencies(this.bundleDependencies);

    MavenArtifactResolvingHelper resolvingHelper = new MavenArtifactResolvingHelper(this.resolver,
            this.repositorySystem,
            this.repositorySystemSession);
    this.remoteRepositories.forEach(resolvingHelper::remoteRepository);

    tool.artifactResolvingHelper(resolvingHelper);

    try {
        File jar = tool.build(this.project.getBuild().getFinalName(), Paths.get(this.projectBuildDir));

        Artifact primaryArtifact = this.project.getArtifact();

        ArtifactHandler handler = new DefaultArtifactHandler("jar");
        Artifact swarmJarArtifact = new DefaultArtifact(
                primaryArtifact.getGroupId(),
                primaryArtifact.getArtifactId(),
                primaryArtifact.getBaseVersion(),
                primaryArtifact.getScope(),
                "jar",
                "swarm",
                handler
        );

        swarmJarArtifact.setFile(jar);

        this.project.addAttachedArtifact(swarmJarArtifact);
    } catch (Exception e) {
        throw new MojoFailureException("Unable to create -swarm.jar", e);
    }
}
 
Example 18
Source File: SystemDependencySkipper.java    From pgpverify-maven-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public boolean shouldSkipArtifact(Artifact artifact) {
    final String artifactScope = artifact.getScope();

    return artifactScope != null && artifactScope.equals(Artifact.SCOPE_SYSTEM);
}
 
Example 19
Source File: ProvidedDependencySkipper.java    From pgpverify-maven-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public boolean shouldSkipArtifact(Artifact artifact) {
    final String artifactScope = artifact.getScope();

    return artifactScope != null && artifactScope.equals(Artifact.SCOPE_PROVIDED);
}
 
Example 20
Source File: Util.java    From jax-maven-plugin with Apache License 2.0 4 votes vote down vote up
private static String string(Artifact a) {
    return a.getGroupId() + ":" + a.getArtifactId() + ":" + a.getVersion() + ":" + a.getScope() + ":" + a.getType();
}