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

The following examples show how to use org.apache.maven.model.Dependency#setArtifactId() . 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: JsonFileDependencyLoader.java    From yaks with Apache License 2.0 6 votes vote down vote up
@Override
protected List<Dependency> load(Path filePath, Properties properties, Logger logger) throws LifecycleExecutionException {
    List<Dependency> dependencyList = new ArrayList<>();

    ObjectMapper mapper = new ObjectMapper();
    try {
        JsonNode root = mapper.readTree(new StringReader(new String(Files.readAllBytes(filePath), StandardCharsets.UTF_8)));
        ArrayNode dependencies = (ArrayNode) root.get("dependencies");
        for (Object o : dependencies) {
            ObjectNode coordinates = (ObjectNode) o;
            Dependency dependency = new Dependency();

            dependency.setGroupId(coordinates.get("groupId").textValue());
            dependency.setArtifactId(coordinates.get("artifactId").textValue());
            dependency.setVersion(resolveVersionProperty(coordinates.get("version").textValue(), properties));

            logger.info(String.format("Add %s", dependency));
            dependencyList.add(dependency);
        }
    } catch (IOException e) {
        throw new LifecycleExecutionException("Failed to read json dependency config file", e);
    }

    return dependencyList;
}
 
Example 2
Source File: MavenUtils.java    From developer-studio with Apache License 2.0 6 votes vote down vote up
public static Dependency createDependency(String groupId, String artifactId,
		String version, String scope, String type, String systemPath) {
	Dependency dependency = new Dependency();
	dependency.setGroupId(groupId);
	dependency.setArtifactId(artifactId);
	if (version!=null) {
		dependency.setVersion(version);
	}
	if (scope!=null) {
		dependency.setScope(scope);
	}
	if (systemPath!=null) {
		dependency.setSystemPath(systemPath);
	}
	if (type!=null) {
		dependency.setType(type);
	}
	return dependency;
}
 
Example 3
Source File: MojoUtils.java    From quarkus with Apache License 2.0 6 votes vote down vote up
public static Dependency parse(String dependency) {
    Dependency res = new Dependency();
    String[] segments = dependency.split(":");
    if (segments.length >= 2) {
        res.setGroupId(segments[0].toLowerCase());
        res.setArtifactId(segments[1].toLowerCase());
        if (segments.length >= 3 && !segments[2].isEmpty()) {
            res.setVersion(segments[2]);
        }
        if (segments.length >= 4) {
            res.setClassifier(segments[3].toLowerCase());
        }
        return res;
    } else {
        throw new IllegalArgumentException("Invalid dependency description '" + dependency + "'");
    }
}
 
Example 4
Source File: MavenPomFileGenerator.java    From Pushjet-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 5
Source File: PomChangePluginTest.java    From butterfly with MIT License 6 votes vote down vote up
@Test
public void addPluginDependencyTest() throws IOException, XmlPullParserException {
    final Dependency dependency = new Dependency();
    dependency.setGroupId("com.test.butterfly");
    dependency.setArtifactId("butterfly-transformation");
    dependency.setVersion("1.0.0-SNAPSHOT");
    List<Dependency> dependencyList = Arrays.asList(dependency);
    PomChangePlugin uut = new PomChangePlugin("org.codehaus.mojo", "cobertura-maven-plugin").relative("pom.xml").setPluginDependencies(dependencyList);

    assertTrue(getPluginBeforeChange(uut).getDependencies().isEmpty());
    executeAndAssertSuccess(uut);
    assertEquals(getPluginAfterChange(uut).getDependencies().size(), dependencyList.size());
    assertEquals(getPluginAfterChange(uut).getDependencies().get(0).getArtifactId(), dependency.getArtifactId());
    assertEquals(getPluginAfterChange(uut).getDependencies().get(0).getGroupId(), dependency.getGroupId());
    assertEquals(getPluginAfterChange(uut).getDependencies().get(0).getVersion(), dependency.getVersion());
}
 
Example 6
Source File: DetectExtension.java    From os-maven-plugin with Apache License 2.0 6 votes vote down vote up
private static void interpolate(Map<String, String> dict, Iterable<Dependency> dependencies) {
    if (dependencies == null) {
        return;
    }

    for (Dependency d: dependencies) {
        d.setGroupId(interpolate(dict, d.getGroupId()));
        d.setArtifactId(interpolate(dict, d.getArtifactId()));
        d.setVersion(interpolate(dict, d.getVersion()));
        d.setClassifier(interpolate(dict, d.getClassifier()));
        d.setSystemPath(interpolate(dict, d.getSystemPath()));
        for (Exclusion e: d.getExclusions()) {
            e.setGroupId(interpolate(dict, e.getGroupId()));
            e.setArtifactId(interpolate(dict, e.getArtifactId()));
        }
    }
}
 
Example 7
Source File: DependencyLoader.java    From yaks with Apache License 2.0 6 votes vote down vote up
/**
 * Construct dependency form coordinate string that follows the format "groupId:artifactId:version". Coordinates must
 * have a version set.
 * @param coordinates
 * @param properties
 * @param logger
 * @return
 */
default Dependency build(String coordinates, Properties properties, Logger logger) throws LifecycleExecutionException {
    Dependency dependency = new Dependency();
    Matcher matcher = COORDINATE_PATTERN.matcher(coordinates);
    if (!matcher.matches()) {
        throw new LifecycleExecutionException("Unsupported dependency coordinate. Must be of format groupId:artifactId:version");
    }

    String groupId = matcher.group("groupId");
    String artifactId = matcher.group("artifactId");
    String version = resolveVersionProperty(matcher.group("version"), properties);
    dependency.setGroupId(groupId);
    dependency.setArtifactId(artifactId);
    dependency.setVersion(version);

    logger.info(String.format("Add %s", dependency));
    return dependency;
}
 
Example 8
Source File: GradleBuildFileFromConnector.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private Dependency gradleModuleVersionToDependency(EclipseExternalDependency eed) {
    Dependency dependency = new Dependency();
    if (eed == null || eed.getGradleModuleVersion() == null) {
        // local dependencies are ignored
        System.err.println("Found null dependency:" + eed);
        return null;
    }
    dependency.setGroupId(eed.getGradleModuleVersion().getGroup());
    dependency.setArtifactId(eed.getGradleModuleVersion().getName());
    dependency.setVersion(eed.getGradleModuleVersion().getVersion());
    return dependency;
}
 
Example 9
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 10
Source File: AddExtensionIT.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Test
void testAddExtensionWithASingleExtension() throws MavenInvocationException, IOException {
    testDir = initProject(PROJECT_SOURCE_DIR, "projects/testAddExtensionWithASingleExtension");
    invoker = initInvoker(testDir);
    addExtension(false, VERTX_ARTIFACT_ID);

    Model model = loadPom(testDir);
    Dependency expected = new Dependency();
    expected.setGroupId(QUARKUS_GROUPID);
    expected.setArtifactId(VERTX_ARTIFACT_ID);
    assertThat(contains(model.getDependencies(), expected)).isTrue();
}
 
Example 11
Source File: Mojo.java    From capsule-maven-plugin with MIT License 5 votes vote down vote up
private Dependency toDependency(final Artifact artifact) {
	if (artifact == null) return null;
	final Dependency dependency = new Dependency();
	dependency.setGroupId(artifact.getGroupId());
	dependency.setArtifactId(artifact.getArtifactId());
	dependency.setVersion(artifact.getVersion());
	dependency.setScope(artifact.getScope());
	dependency.setClassifier(artifact.getClassifier());
	dependency.setOptional(artifact.isOptional());
	if (dependency.getScope() == null || dependency.getScope().isEmpty()) dependency.setScope("compile");
	return dependency;
}
 
Example 12
Source File: DependencyResolver.java    From smart-testing with Apache License 2.0 5 votes vote down vote up
private Dependency smartTestingProviderDependency() {
    final Dependency smartTestingSurefireProvider = new Dependency();
    smartTestingSurefireProvider.setGroupId("org.arquillian.smart.testing");
    smartTestingSurefireProvider.setArtifactId("surefire-provider");
    smartTestingSurefireProvider.setVersion(ExtensionVersion.version().toString());
    smartTestingSurefireProvider.setScope("runtime");
    smartTestingSurefireProvider.setClassifier("shaded");
    return smartTestingSurefireProvider;
}
 
Example 13
Source File: MvnProjectBuilder.java    From quarkus with Apache License 2.0 5 votes vote down vote up
public MvnProjectBuilder addDependency(String artifactId, String scope) {
    final Dependency dep = new Dependency();
    dep.setGroupId(DEFAULT_GROUP_ID);
    dep.setArtifactId(artifactId);
    dep.setVersion(DEFAULT_VERSION);
    dep.setScope(scope);
    return addDependency(dep);
}
 
Example 14
Source File: DependencyAddedInProfileTest.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
protected TsArtifact modelApp() {

    final TsQuarkusExt extA_100 = new TsQuarkusExt("ext-a", "1.0.0");
    addToExpectedLib(extA_100.getRuntime());

    final TsQuarkusExt extB_100 = new TsQuarkusExt("ext-b", "1.0.0");
    install(extB_100);
    final TsArtifact extB_100_rt = extB_100.getRuntime();
    addToExpectedLib(extB_100_rt);

    final TsArtifact appJar = TsArtifact.jar("app")
            .addDependency(extA_100);

    final Profile profile = new Profile();
    profile.setId("extra");
    Activation activation = new Activation();
    ActivationProperty ap = new ActivationProperty();
    ap.setName("extra");
    activation.setProperty(ap);
    profile.setActivation(activation);
    final Dependency dep = new Dependency();
    dep.setGroupId(extB_100_rt.getGroupId());
    dep.setArtifactId(extB_100_rt.getArtifactId());
    dep.setVersion(extB_100_rt.getVersion());
    profile.addDependency(dep);
    appJar.addProfile(profile);

    createWorkspace();

    setSystemProperty("extra", "extra");

    return appJar;
}
 
Example 15
Source File: AddGradleExtensionsTest.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
protected List<Dependency> getDependencies() throws IOException {
    final Matcher matcher = Pattern.compile("\\s*implementation\\s+'([^\\v:]+):([^\\v:]+)(:[^:\\v]+)?'")
            .matcher(getBuildContent());
    final ArrayList<Dependency> builder = new ArrayList<>();
    while (matcher.find()) {
        final Dependency dep = new Dependency();
        dep.setGroupId(matcher.group(1));
        dep.setArtifactId(matcher.group(2));
        dep.setVersion(matcher.group(3));
        builder.add(dep);
    }
    return builder;
}
 
Example 16
Source File: QuarkusTestPlatformDescriptorLoader.java    From quarkus with Apache License 2.0 5 votes vote down vote up
public static void addExtension(String groupId, String artifactId, String version, String name, String guide,
        List<Extension> extensions, List<Dependency> bomDeps) {
    extensions.add(new Extension(groupId, artifactId, version).setName(name).setGuide(guide));

    final Dependency d = new Dependency();
    d.setGroupId(groupId);
    d.setArtifactId(artifactId);
    d.setVersion(version);
    bomDeps.add(d);
}
 
Example 17
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 18
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;
}
 
Example 19
Source File: RelocationManipulator.java    From pom-manipulation-ext with Apache License 2.0 4 votes vote down vote up
private boolean updateDependencies( WildcardMap<ProjectVersionRef> relocations, Map<ArtifactRef, Dependency> dependencies )
{
    final Map<ArtifactRef, Dependency> postFixUp = new HashMap<>();
    boolean result = false;

    // If we do a single pass over the dependencies that will handle the relocations *but* it will not handle
    // where one relocation alters the dependency and a subsequent relocation alters it again. For instance, the
    // first might wildcard alter the groupId and the second, more specifically alters one with the artifactId
    for ( int i = 0 ; i < relocations.size(); i++ )
    {
        Iterator<ArtifactRef> it = dependencies.keySet().iterator();
        while ( it.hasNext() )
        {
            final ArtifactRef pvr = it.next();
            if ( relocations.containsKey( pvr.asProjectRef() ) )
            {
                ProjectVersionRef relocation = relocations.get( pvr.asProjectRef() );

                updateDependencyExclusion( pvr, relocation );

                Dependency dependency = dependencies.get( pvr );

                logger.info( "Replacing groupId {} by {} and artifactId {} with {}",
                             dependency.getGroupId(), relocation.getGroupId(), dependency.getArtifactId(), relocation.getArtifactId() );

                if ( !relocation.getArtifactId().equals( WildcardMap.WILDCARD ) )
                {
                    dependency.setArtifactId( relocation.getArtifactId() );
                }
                dependency.setGroupId( relocation.getGroupId() );

                // Unfortunately because we iterate using the resolved project keys if the relocation updates those
                // keys multiple iterations will not work. Therefore we need to remove the original key:dependency
                // to map to the relocated form.
                postFixUp.put( new SimpleScopedArtifactRef( dependency ), dependency );
                it.remove();

                result = true;
            }
        }
        dependencies.putAll( postFixUp );
        postFixUp.clear();
    }
    return result;
}
 
Example 20
Source File: MavenUtils.java    From developer-studio with Apache License 2.0 4 votes vote down vote up
public static void updateDependecyList(IProject project, MavenProject mavenProject) throws Exception {
		List<Dependency> existingDependencies = mavenProject.getModel().getDependencies();
//		List<String> newDependencyStrings=new ArrayList<String>();
//		List<String> existingDependencyStrings=new ArrayList<String>();
		List<Dependency> newDependencyList = new ArrayList<Dependency>();
		
		Map<String, JavaLibraryBean> dependencyInfoMap = JavaLibraryUtil.getDependencyInfoMap(project);
		Map<String, String> map = ProjectDependencyConstants.DEPENDENCY_MAP;
		for (JavaLibraryBean bean : dependencyInfoMap.values()) {
			if (bean.getVersion().contains("${")){
				for(String path: map.keySet()) {
					bean.setVersion(bean.getVersion().replace(path, map.get(path)));
				}
			}
			Dependency dependency = new Dependency();
			dependency.setArtifactId(bean.getArtifactId());
			dependency.setGroupId(bean.getGroupId());
			dependency.setVersion(bean.getVersion());
//			String dependencyString = getDependencyString(dependency);
//			newDependencyStrings.add(dependencyString);
			newDependencyList.add(dependency);
//			if(!dependencies.contains(dependency)){
//				dependencies.add(dependency);
//			}
		}
		
//		for (Dependency dependency : existingDependencies) {
//			String dependencyString = getDependencyString(dependency);
//			existingDependencyStrings.add(dependencyString);
//		}
		
		for (Dependency newDependency : newDependencyList) {
			boolean found=false;
			for (Dependency existingDependency : existingDependencies) {
				if(newDependency.getArtifactId().equals(existingDependency.getArtifactId()) &&
						newDependency.getGroupId().equals(existingDependency.getGroupId()) &&
						newDependency.getVersion().equals(existingDependency.getVersion())){
					found = true;
				}
			}
			if(!found){
				existingDependencies.add(newDependency);
			}
		}
		
//		for (Dependency dependency : newDependencyList) {
//			String dependencyString = getDependencyString(dependency);
//			if (!newDependencyStrings.contains(dependencyString)){
//				existingDependencies.add(dependency);
//			}
//		}
		addMavenDependency(mavenProject, existingDependencies);
	}