Java Code Examples for org.apache.maven.model.Dependency#setType()

The following examples show how to use org.apache.maven.model.Dependency#setType() . 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: AbstractXJC2Mojo.java    From maven-jaxb2-plugin with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public List<Dependency> getProjectDependencies() {
	
	@SuppressWarnings("unchecked")
	final Set<Artifact> artifacts = getProject().getArtifacts();
	
	if (artifacts == null) {
		return Collections.emptyList();
	} else {
		final List<Dependency> dependencies = new ArrayList<Dependency>(artifacts.size());
		for (Artifact artifact : artifacts) {
			final Dependency dependency = new Dependency();
			dependency.setGroupId(artifact.getGroupId());
			dependency.setArtifactId(artifact.getArtifactId());
			dependency.setVersion(artifact.getVersion());
			dependency.setClassifier(artifact.getClassifier());
			dependency.setScope(artifact.getScope());
			dependency.setType(artifact.getType());
			dependencies.add(dependency);
		}
		return dependencies;
	}
}
 
Example 2
Source File: MavenPomFileGenerator.java    From pushfish-android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void addDependency(MavenDependencyInternal dependency, String artifactId, String scope, String type, String classifier) {
    Dependency mavenDependency = new Dependency();
    mavenDependency.setGroupId(dependency.getGroupId());
    mavenDependency.setArtifactId(artifactId);
    mavenDependency.setVersion(dependency.getVersion());
    mavenDependency.setType(type);
    mavenDependency.setScope(scope);
    mavenDependency.setClassifier(classifier);

    for (ExcludeRule excludeRule : dependency.getExcludeRules()) {
        Exclusion exclusion = new Exclusion();
        exclusion.setGroupId(GUtil.elvis(excludeRule.getGroup(), "*"));
        exclusion.setArtifactId(GUtil.elvis(excludeRule.getModule(), "*"));
        mavenDependency.addExclusion(exclusion);
    }

    getModel().addDependency(mavenDependency);
}
 
Example 3
Source File: ExternalBomResolver.java    From sundrio with Apache License 2.0 6 votes vote down vote up
private Dependency toMavenDependency(org.eclipse.aether.graph.Dependency from) {
    org.eclipse.aether.artifact.Artifact fromArt = from.getArtifact();

    Dependency dependency = new Dependency();
    dependency.setGroupId(fromArt.getGroupId());
    dependency.setArtifactId(fromArt.getArtifactId());
    dependency.setVersion(fromArt.getVersion());
    dependency.setType(fromArt.getExtension());
    dependency.setScope(Artifact.SCOPE_COMPILE.equals(from.getScope()) ? null : from.getScope());
    dependency.setClassifier(fromArt.getClassifier());
    dependency.setOptional(dependency.isOptional());

    if (from.getExclusions() != null && from.getExclusions().size() > 0) {
        List<Exclusion> exclusions = new LinkedList<Exclusion>();
        for (org.eclipse.aether.graph.Exclusion fromEx : from.getExclusions()) {
            Exclusion ex = new Exclusion();
            ex.setGroupId(fromEx.getGroupId());
            ex.setArtifactId(fromEx.getArtifactId());
            exclusions.add(ex);
        }
        dependency.setExclusions(exclusions);
    }

    return dependency;
}
 
Example 4
Source File: ArtifactUtils.java    From maven-jaxb2-plugin with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static void mergeDependencyWithDefaults(Dependency dep, Dependency def) {
	if (dep.getScope() == null && def.getScope() != null) {
		dep.setScope(def.getScope());
		dep.setSystemPath(def.getSystemPath());
	}

	if (dep.getVersion() == null && def.getVersion() != null) {
		dep.setVersion(def.getVersion());
	}

	if (dep.getClassifier() == null && def.getClassifier() != null) {
		dep.setClassifier(def.getClassifier());
	}

	if (dep.getType() == null && def.getType() != null) {
		dep.setType(def.getType());
	}

	@SuppressWarnings("rawtypes")
	List exclusions = dep.getExclusions();
	if (exclusions == null || exclusions.isEmpty()) {
		dep.setExclusions(def.getExclusions());
	}
}
 
Example 5
Source File: DefaultPomDependenciesConverter.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private Dependency createMavenDependency(ModuleDependency dependency, String name, String type, String scope, String classifier,
        Set<Configuration> configurations) {
    Dependency mavenDependency =  new Dependency();
    mavenDependency.setGroupId(dependency.getGroup());
    if (dependency instanceof ProjectDependency) {
        mavenDependency.setArtifactId(determineProjectDependencyArtifactId((ProjectDependency) dependency));
    } else {
        mavenDependency.setArtifactId(name);
    }
    mavenDependency.setVersion(dependency.getVersion());
    mavenDependency.setType(type);
    mavenDependency.setScope(scope);
    mavenDependency.setOptional(false);
    mavenDependency.setClassifier(classifier);
    mavenDependency.setExclusions(getExclusions(dependency, configurations));
    return mavenDependency;
}
 
Example 6
Source File: DefaultPomDependenciesConverter.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private Dependency createMavenDependency(ModuleDependency dependency, String name, String type, String scope, String classifier,
        Set<Configuration> configurations) {
    Dependency mavenDependency =  new Dependency();
    mavenDependency.setGroupId(dependency.getGroup());
    if (dependency instanceof ProjectDependency) {
        mavenDependency.setArtifactId(determineProjectDependencyArtifactId((ProjectDependency) dependency));
    } else {
        mavenDependency.setArtifactId(name);
    }
    mavenDependency.setVersion(dependency.getVersion());
    mavenDependency.setType(type);
    mavenDependency.setScope(scope);
    mavenDependency.setOptional(false);
    mavenDependency.setClassifier(classifier);
    mavenDependency.setExclusions(getExclusions(dependency, configurations));
    return mavenDependency;
}
 
Example 7
Source File: FlattenModelResolver.java    From flatten-maven-plugin with Apache License 2.0 6 votes vote down vote up
private String resolveParentVersionRange( String groupId, String artifactId, String version )
    throws UnresolvableModelException
{

    Dependency parentDependency = new Dependency();
    parentDependency.setGroupId( groupId );
    parentDependency.setArtifactId( artifactId );
    parentDependency.setVersion( version );
    parentDependency.setClassifier( "" );
    parentDependency.setType( "pom" );

    try
    {
        Iterable<ArtifactResult> artifactResults = depencencyResolver.resolveDependencies(
            projectBuildingRequest, singleton( parentDependency ), null, null );
        return artifactResults.iterator().next().getArtifact().getVersion();
    }
    catch ( DependencyResolverException e )
    {
        throw new UnresolvableModelException( e.getMessage(), groupId, artifactId, version, e );
    }
}
 
Example 8
Source File: TestHelper.java    From yaks with Apache License 2.0 6 votes vote down vote up
/**
 * Verify that default mock dependencies are present in the given list od dependencies. This verification can be shared by multiple
 * tests that load the dependency list in different ways (e.g. via Json, Yaml, System properties, ...)
 * @param dependencyList
 */
public static void verifyDependencies(List<Dependency> dependencyList) {
    Dependency foo = new Dependency();
    foo.setGroupId("org.foo");
    foo.setArtifactId("foo-artifact");
    foo.setVersion("1.0.0");
    foo.setType("jar");

    Dependency bar = new Dependency();
    bar.setGroupId("org.bar");
    bar.setArtifactId("bar-artifact");
    bar.setVersion("1.5.0");
    bar.setType("jar");

    Assertions.assertThat(dependencyList).hasSize(2);
    Assertions.assertThat(dependencyList).anyMatch(dependency -> dependency.toString().equals(foo.toString()));
    Assertions.assertThat(dependencyList).anyMatch(dependency -> dependency.toString().equals(bar.toString()));
}
 
Example 9
Source File: MavenCoordinatesResolver.java    From smart-testing with Apache License 2.0 5 votes vote down vote up
static Dependency createDependencyFromCoordinates(String coordinatesString, boolean excludeTransitive) {
    final String[] coordinates = coordinatesString.split(":");
    int amountOfCoordinates = coordinates.length;
    if (amountOfCoordinates < 2) {
        throw new IllegalArgumentException(
            "Coordinates of the specified strategy [" + coordinatesString + "] doesn't have the correct format.");
    }
    final Dependency dependency = new Dependency();
    dependency.setGroupId(coordinates[0]);
    dependency.setArtifactId(coordinates[1]);
    if (amountOfCoordinates == 3) {
        dependency.setVersion(coordinates[2]);
    } else if (amountOfCoordinates >= 4) {
        dependency.setType(coordinates[2].isEmpty() ? "jar" : coordinates[2]);
        dependency.setClassifier(coordinates[3]);
    }
    if (amountOfCoordinates >= 5) {
        dependency.setVersion(coordinates[4]);
    }
    if (amountOfCoordinates == 6) {
        dependency.setScope(coordinates[5]);
    }
    if (dependency.getVersion() == null || dependency.getVersion().isEmpty()) {
        dependency.setVersion(ExtensionVersion.version().toString());
    }
    if (excludeTransitive) {
        Exclusion exclusion = new Exclusion();
        exclusion.setGroupId("*");
        exclusion.setArtifactId("*");
        dependency.setExclusions(Arrays.asList(exclusion));
    }
    return dependency;
}
 
Example 10
Source File: GenerateBomMojo.java    From sundrio with Apache License 2.0 5 votes vote down vote up
private static Dependency toDependency(Artifact artifact) {
    Dependency dependency = new Dependency();
    dependency.setGroupId(artifact.getGroupId());
    dependency.setArtifactId(artifact.getArtifactId());
    dependency.setVersion(artifact.getVersion());
    dependency.setType(artifact.getType());
    dependency.setScope(Artifact.SCOPE_COMPILE.equals(artifact.getScope()) ? null : artifact.getScope());
    dependency.setClassifier(artifact.getClassifier());
    dependency.setOptional(artifact.isOptional());
    return dependency;
}
 
Example 11
Source File: MavenArtifactResolvingHelperTest.java    From thorntail with Apache License 2.0 5 votes vote down vote up
private Dependency createDependency(String type) {
    Dependency dependency = new Dependency();
    dependency.setGroupId("io.thorntail");
    dependency.setArtifactId("test");
    dependency.setVersion("1.0");
    dependency.setType(type);
    return dependency;
}
 
Example 12
Source File: DependencyToStringTest.java    From unleash-maven-plugin with Eclipse Public License 1.0 5 votes vote down vote up
private static Dependency createDependency(String gid, String aid, String version, String type, String classifier) {
  Dependency d = new Dependency();
  d.setGroupId(gid);
  d.setArtifactId(aid);
  d.setVersion(version);
  d.setType(type);
  d.setClassifier(classifier);
  return d;
}
 
Example 13
Source File: BootstrapMojo.java    From grpc-java-contrib with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Creates a dependency artifact from a specification in
 * {@code groupId:artifactId:version[:type[:classifier]]} format.
 *
 * @return artifact object instance.
 */
private Artifact createDependencyArtifact(String groupId, String artifactId, String version, String type, String classifier) {
    Dependency dependency = new Dependency();
    dependency.setGroupId(groupId);
    dependency.setArtifactId(artifactId);
    dependency.setVersion(version);
    dependency.setType(type);
    dependency.setClassifier(classifier);
    dependency.setScope(Artifact.SCOPE_RUNTIME);
    return repositorySystem.createDependencyArtifact(dependency);
}
 
Example 14
Source File: PomChangeDependency.java    From butterfly with MIT License 5 votes vote down vote up
@Override
protected TOExecutionResult pomExecution(String relativePomFile, Model model) {
    TOExecutionResult result;

    Dependency dependency = getDependency(model, groupId, artifactId);
    if (dependency != null) {
        model.removeDependency(dependency);

        if (removeVersion) dependency.setVersion(null); else if (version != null) dependency.setVersion(version);
        if (removeScope) dependency.setScope(null); else if (scope != null) dependency.setScope(scope);
        if (removeType) dependency.setType(null); else if (type != null) dependency.setType(type);
        if (removeOptional) dependency.setOptional(null); else if (optional != null) dependency.setOptional(optional);

        model.addDependency(dependency);

        String details = String.format("Dependency %s:%s has been changed in %s", groupId, artifactId, getRelativePath());
        result = TOExecutionResult.success(this, details);
    } else {
        String message = String.format("Dependency %s:%s is not present in %s", groupId, artifactId, getRelativePath());

        switch (ifNotPresent) {
            case Warn:
                result = TOExecutionResult.warning(this, new TransformationOperationException(message));
                break;
            case NoOp:
                result = TOExecutionResult.noOp(this, message);
                break;
            case Fail:
                // Fail is the default
            default:
                result = TOExecutionResult.error(this, new TransformationOperationException(message));
                break;
        }
    }

    return result;
}
 
Example 15
Source File: MavenHelper.java    From microprofile-starter with Apache License 2.0 5 votes vote down vote up
public void addDependency(Model pomFile, String groupId, String artifactId, String version, String scope, String type) {
    Dependency dependency = new Dependency();
    dependency.setGroupId(groupId);
    dependency.setArtifactId(artifactId);
    dependency.setVersion(version);
    if (scope != null) {

        dependency.setScope(scope);
    }
    if (type != null) {
        dependency.setType(type);
    }
    pomFile.addDependency(dependency);

}
 
Example 16
Source File: UseLatestReleasesMojo.java    From versions-maven-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * @param pom the pom to update.
 * @throws org.apache.maven.plugin.MojoExecutionException when things go wrong
 * @throws org.apache.maven.plugin.MojoFailureException when things go wrong in a very bad way
 * @throws javax.xml.stream.XMLStreamException when things go wrong with XML streaming
 * @see org.codehaus.mojo.versions.AbstractVersionsUpdaterMojo#update(org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader)
 */
protected void update( ModifiedPomXMLEventReader pom )
    throws MojoExecutionException, MojoFailureException, XMLStreamException
{
    try
    {
        if ( getProject().getDependencyManagement() != null && isProcessingDependencyManagement() )
        {
            useLatestReleases( pom, getProject().getDependencyManagement().getDependencies() );
        }
        if ( getProject().getDependencies() != null && isProcessingDependencies() )
        {
            useLatestReleases( pom, getProject().getDependencies() );
        }
        if ( getProject().getParent() != null && isProcessingParent() )
        {
            Dependency dependency = new Dependency();
            dependency.setArtifactId(getProject().getParent().getArtifactId());
            dependency.setGroupId(getProject().getParent().getGroupId());
            dependency.setVersion(getProject().getParent().getVersion());
            dependency.setType("pom");
            List list = new ArrayList();
            list.add(dependency);
            useLatestReleases( pom, list);
        }
    }
    catch ( ArtifactMetadataRetrievalException e )
    {
        throw new MojoExecutionException( e.getMessage(), e );
    }
}
 
Example 17
Source File: MavenBuildFile.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
protected void addDependencyInBuildFile(AppArtifactCoords coords) throws IOException {
    if (getModel() != null) {
        final Dependency d = new Dependency();
        d.setGroupId(coords.getGroupId());
        d.setArtifactId(coords.getArtifactId());
        d.setVersion(coords.getVersion());
        // When classifier is empty, you get  <classifier></classifier> in the pom.xml
        if (coords.getClassifier() != null && !coords.getClassifier().isEmpty()) {
            d.setClassifier(coords.getClassifier());
        }
        d.setType(coords.getType());
        getModel().addDependency(d);
    }
}
 
Example 18
Source File: MavenUtil.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
public static Dependency newDependency(final String groupId, final String artifactId, final String version, final String classifier, final String type) {
  final Dependency dependency = new Dependency();
  dependency.setGroupId(groupId);
  dependency.setArtifactId(artifactId);
  dependency.setVersion(version);
  dependency.setClassifier(classifier);
  if (type != null)
    dependency.setType(type);

  return dependency;
}
 
Example 19
Source File: MavenPomFileGenerator.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void addDependency(MavenDependencyInternal dependency, String artifactId, String scope, String type, String classifier) {
    Dependency mavenDependency = new Dependency();
    mavenDependency.setGroupId(dependency.getGroupId());
    mavenDependency.setArtifactId(artifactId);
    mavenDependency.setVersion(dependency.getVersion());
    mavenDependency.setType(type);
    mavenDependency.setScope(scope);
    mavenDependency.setClassifier(classifier);

    getModel().addDependency(mavenDependency);
}
 
Example 20
Source File: PomHelper.java    From versions-maven-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * Reads imported POMs from the dependency management section.
 *
 * @param pom POM
 * @return a non-null list of {@link Dependency} for each imported POM
 * @throws XMLStreamException XML stream exception
 * @see <a href="https://github.com/mojohaus/versions-maven-plugin/issues/134">bug #134</a>
 * @since 2.4
 */
public static List<Dependency> readImportedPOMsFromDependencyManagementSection( ModifiedPomXMLEventReader pom )
    throws XMLStreamException
{
    List<Dependency> importedPOMs = new ArrayList<Dependency>();
    Stack<String> stack = new Stack<String>();

    String groupIdElement = "groupId";
    String artifactIdElement = "artifactId";
    String versionElement = "version";
    String typeElement = "type";
    String scopeElement = "scope";
    Set<String> recognizedElements =
        new HashSet<>( Arrays.asList( groupIdElement, artifactIdElement, versionElement, typeElement,
                                            scopeElement ) );
    Map<String, String> depData = new HashMap<>();

    pom.rewind();

    String depMgmtDependencyPath = "/project/dependencyManagement/dependencies/dependency";

    while ( pom.hasNext() )
    {
        XMLEvent event = pom.nextEvent();

        if ( event.isStartElement() )
        {
            final String elementName = event.asStartElement().getName().getLocalPart();
            String parent = "";
            if ( !stack.isEmpty() )
            {
                parent = stack.peek();
            }
            String currentPath = parent + "/" + elementName;
            stack.push( currentPath );

            if ( currentPath.startsWith( depMgmtDependencyPath ) && recognizedElements.contains( elementName ) )
            {
                final String elementText = pom.getElementText().trim();
                depData.put( elementName, elementText );
                stack.pop();
            }
        }
        if ( event.isEndElement() )
        {
            String path = stack.pop();
            if ( depMgmtDependencyPath.equals( path ) )
            {
                if ( "pom".equals( depData.get( typeElement ) ) && "import".equals( depData.get( scopeElement ) ) )
                {
                    Dependency dependency = new Dependency();
                    dependency.setGroupId( depData.get( groupIdElement ) );
                    dependency.setArtifactId( depData.get( artifactIdElement ) );
                    dependency.setVersion( depData.get( versionElement ) );
                    dependency.setType( depData.get( typeElement ) );
                    dependency.setScope( depData.get( scopeElement ) );
                    importedPOMs.add( dependency );
                }
                depData.clear();
            }
        }
    }
    return importedPOMs;
}