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

The following examples show how to use org.apache.maven.artifact.Artifact#isOptional() . These examples are extracted from open source projects. 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
/**
 * Returns a list of {@code URL} objects representing paths of
 * {@link Artifact} objects marked with {@code <optional>true</optional>} in
 * the specified iterator.
 *
 * @param localRepository The local {@link ArtifactRepository}.
 * @param iterator The {@code Iterator} of {@code Artifact} objects.
 * @param depth The depth value for stack tracking (must be called with 0).
 * @return A list of dependency paths in the specified {@code iterator}.
 */
private static URL[] getDependencyPaths(final ArtifactRepository localRepository, final String scope, final boolean optional, final Iterator<Artifact> iterator, final int depth) {
  while (iterator.hasNext()) {
    final Artifact dependency = iterator.next();
    if (optional == dependency.isOptional() && (scope == null || scope.equals(dependency.getScope()))) {
      final URL url = AssembleUtil.toURL(MavenUtil.getPathOf(localRepository.getBasedir(), dependency));
      final URL[] urls = getDependencyPaths(localRepository, scope, optional, iterator, depth + 1);
      if (urls != null && url != null)
        urls[depth] = url;

      return urls;
    }
  }

  return depth == 0 ? null : new URL[depth];
}
 
Example 2
private Comparator<Artifact> getArtifactComparator()
{
    return ( Artifact a1, Artifact a2 ) -> 
    {
        // put optional last
        if ( a1.isOptional() && !a2.isOptional() )
        {
            return +1;
        }
        else if ( !a1.isOptional() && a2.isOptional() )
        {
            return -1;
        }
        else
        {
            return a1.compareTo( a2 );
        }
    };
}
 
Example 3
@Override
public String render(DependencyNode node) {
  Artifact artifact = node.getArtifact();
  ArtifactData artifactData = new ArtifactData(
      this.showGroupId ? artifact.getGroupId() : null,
      this.showArtifactId ? artifact.getArtifactId() : null,
      this.showVersion ? node.getEffectiveVersion() : null,
      this.showOptional ? artifact.isOptional() : null,
      this.showClassifiers ? node.getClassifiers() : emptyList(),
      !node.getScopes().isEmpty() ? node.getScopes() : singletonList(SCOPE_COMPILE),
      this.showTypes ? node.getTypes() : emptyList());

  StringWriter jsonStringWriter = new StringWriter();
  try {
    this.objectMapper.writer().writeValue(jsonStringWriter, artifactData);
  } catch (IOException e) {
    // should never happen with StringWriter
    throw new IllegalStateException(e);
  }

  return jsonStringWriter.toString();
}
 
Example 4
@Override
public String render(DependencyNode node) {
  Artifact artifact = node.getArtifact();

  String artifactString = COLON_JOINER.join(
      this.showGroupId ? artifact.getGroupId() : null,
      this.showArtifactId ? artifact.getArtifactId() : null,
      this.showVersion ? node.getEffectiveVersion() : null,
      this.showTypes ? SLASH_JOINER.join(node.getTypes()) : null,
      this.showClassifiers ? SLASH_JOINER.join(node.getClassifiers()) : null,
      createScopeString(node.getScopes()));

  if (this.showOptional && artifact.isOptional()) {
    return artifactString + " (optional)";
  }

  return artifactString;
}
 
Example 5
private String[] getArtifactRow( Artifact artifact )
{
    return new String[] {
      		artifact.getGroupId(), 
      		artifact.getArtifactId(), 
      		artifact.getVersion(),
      		artifact.getClassifier(), 
      		artifact.getType(), 
      		artifact.isOptional() ? "(optional)" : " "
    			};
}
 
Example 6
private Set<Artifact> filterArtifacts( Set<Artifact> dependencies )
{
    if ( includes == null && excludes == null && ignoredScopes == null && ignoreOptionals == false )
    {
        return dependencies;
    }

    AndArtifactFilter filter = new AndArtifactFilter();
    if ( includes != null )
    {
        filter.add( new StrictPatternIncludesArtifactFilter( includes ) );
    }
    if ( excludes != null )
    {
        filter.add( new StrictPatternExcludesArtifactFilter( excludes ) );
    }

    Set<Artifact> result = new HashSet<Artifact>();
    for ( Artifact artifact : dependencies )
    {
        if ( ignoredScopes != null && Arrays.asList( ignoredScopes ).contains( artifact.getScope() ) )
        {
            continue;
        }
        if ( ignoreOptionals && artifact.isOptional() )
        {
            continue;
        }
        if ( filter.include( artifact ) )
        {
            result.add( artifact );
        }
    }
    return result;
}
 
Example 7
Source Project: capsule-maven-plugin   File: CapsuleMojo.java    License: 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 8
Source Project: japicmp   File: JApiCmpMojo.java    License: Apache License 2.0 5 votes vote down vote up
private void handleMissingArtifactFile(PluginParameters pluginParameters, Artifact artifact) {
	if (artifact.isOptional()) {
		if (pluginParameters.getParameterParam().isIgnoreMissingOptionalDependency()) {
			getLog().info("Ignoring missing optional dependency: " + toDescriptor(artifact));
		} else {
			getLog().warn("Could not resolve optional artifact: " + toDescriptor(artifact));
		}
	} else {
		getLog().warn("Could not resolve artifact: " + toDescriptor(artifact));
	}
}
 
Example 9
Source Project: maven-native   File: NativeLinkMojo.java    License: 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 10
Source Project: sundrio   File: OptionalFilter.java    License: Apache License 2.0 4 votes vote down vote up
public Artifact apply(Artifact artifact) {
    return artifact == null ? null : (artifact.isOptional() ? null : artifact);
}
 
Example 11
@Override
public boolean include(Artifact artifact) {
  return !artifact.isOptional();
}