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

The following examples show how to use org.apache.maven.artifact.Artifact#setScope() . 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: 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 2
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 3
Source File: PluginDescriptorMojoTest.java    From takari-lifecycle with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testSourcepathDependency() throws Exception {
  // assert that MojoAnnotationProcessorMojo honours sourcepath=reactorDependencies

  File basedir = resources.getBasedir("plugin-descriptor/sourcepath-dependency");
  File dependency = new File(basedir, "dependency");
  File plugin = new File(basedir, "plugin");

  MavenProject dependencyProject = mojos.readMavenProject(dependency);
  MavenProject pluginProject = mojos.readMavenProject(plugin);
  addDependencies(pluginProject, "apache-plugin-annotations-jar", "maven-plugin-api-jar");
  mojos.newDependency(new File(dependencyProject.getBuild().getOutputDirectory())) //
      .setGroupId(dependencyProject.getGroupId()) //
      .setArtifactId(dependencyProject.getArtifactId()) //
      .setVersion(dependencyProject.getVersion()) //
      .addTo(pluginProject);
  Artifact dependencyArtifact = dependencyProject.getArtifact();
  dependencyArtifact.setScope(Artifact.SCOPE_COMPILE);
  pluginProject.getArtifacts().add(dependencyArtifact);
  MavenSession session = mojos.newMavenSession(pluginProject);
  session.setProjects(ImmutableList.of(dependencyProject, pluginProject));
  session.setCurrentProject(pluginProject);
  mojos.executeMojo(session, pluginProject, "mojo-annotation-processor", newParameter("sourcepath", "reactorDependencies"));
  mojos.executeMojo(session, pluginProject, "plugin-descriptor");
  mojos.assertBuildOutputs(plugin, "target/classes/META-INF/maven/plugin.xml", "target/classes/META-INF/m2e/lifecycle-mapping-metadata.xml");
}
 
Example 4
Source File: DependencyEmbedderTest.java    From wisdom with Apache License 2.0 6 votes vote down vote up
@Test
public void testScopeFiltering() {
    Properties instructions = new Properties();
    instructions.setProperty(DependencyEmbedder.EMBED_DEPENDENCY, "acme:*;scope=provided");
    DependencyEmbedder dependencyEmbedder = new DependencyEmbedder(instructions, reporter);

    final Artifact acme = create("acme", "acme-sample", "1");
    acme.setScope("provided");
    final Artifact acme2 = create("acme", "acme-sample-2", "1");
    acme2.setScope("compile");
    ProjectDependencies dependencies = new ProjectDependencies(
            ImmutableList.of(create("org.wisdom-framework", "wisdom-api", "1"),
                    acme, acme2),
            ImmutableList.of(create("org.wisdom-framework", "wisdom-api", "1"),
                    acme, acme2)
    );
    Properties properties = dependencyEmbedder.generate(instructions, dependencies);

    assertThat(properties.getProperty(Constants.INCLUDE_RESOURCE))
            .contains("@" + acme.getFile().getAbsolutePath())
            .doesNotContain("@" + acme2.getFile().getAbsolutePath());

    assertThat(properties.getProperty(Constants.BUNDLE_CLASSPATH)).isNull();
}
 
Example 5
Source File: A_ModuleGenerator.java    From deadcode4j with Apache License 2.0 6 votes vote down vote up
@Test
public void createsClassPathEntryForKnownProject() throws MojoExecutionException {
    MavenProject firstProject = givenMavenProject("firstProject");
    Artifact artifact = new ArtifactStub();
    artifact.setGroupId("de.is24.junit");
    artifact.setArtifactId("firstProject");
    artifact.setVersion("42");
    artifact.setScope("compile");
    mavenProject.setArtifacts(newHashSet(artifact));

    Iterable<Module> modules = objectUnderTest.getModulesFor(asList(firstProject, mavenProject));

    assertThat(modules, is(Matchers.<Module>iterableWithSize(2)));
    Module module = Iterables.getLast(modules);
    assertThat(module.getClassPath(), is(Matchers.<File>iterableWithSize(1)));
}
 
Example 6
Source File: DependencyNodeTest.java    From depgraph-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Test
void effectiveScope() {
  // arrange
  Artifact artifact1 = createMavenArtifact();
  artifact1.setScope("runtime");
  Artifact artifact2 = createMavenArtifact();
  artifact2.setScope("provided");

  // act
  DependencyNode node = new DependencyNode(artifact1);
  node.merge(new DependencyNode(artifact2));

  // assert
  assertEquals("provided", node.getEffectiveScope());
}
 
Example 7
Source File: DependencyNodeTest.java    From depgraph-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Test
void effectiveScopeForNull() {
  Artifact artifact = createMavenArtifact();
  artifact.setScope(null);

  DependencyNode adapter = new DependencyNode(artifact);
  assertEquals("compile", adapter.getEffectiveScope());
}
 
Example 8
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 9
Source File: A_ModuleGenerator.java    From deadcode4j with Apache License 2.0 5 votes vote down vote up
private Artifact addArtifact(MavenProject mavenProject, final boolean resolved) {
    Artifact artifact = new ArtifactStub() {
        private boolean resolved = false;

        @Override
        public boolean isResolved() {
            return this.resolved;
        }

        @Override
        public void setResolved(boolean b) {
            this.resolved = b;
        }

        @Override
        public File getFile() {
            return isResolved() ? super.getFile() : null;
        }
    };
    artifact.setGroupId("de.is24.junit");
    artifact.setArtifactId("dependency");
    artifact.setVersion("42");
    artifact.setScope("compile");
    artifact.setResolved(resolved);
    artifact.setFile(tempFileRule.getTempFile());

    mavenProject.setArtifactFilter(new ScopeArtifactFilter(SCOPE_COMPILE_PLUS_RUNTIME));
    if (resolved) {
        mavenProject.setResolvedArtifacts(newHashSet(artifact));
    } else {
        mavenProject.setArtifacts(newHashSet(artifact));
    }
    return artifact;
}
 
Example 10
Source File: DependencyEmbedderTest.java    From wisdom with Apache License 2.0 4 votes vote down vote up
@Test
public void testGroupIdAndVersionFiltering() {
    Properties instructions = new Properties();
    instructions.setProperty(DependencyEmbedder.EMBED_DEPENDENCY, "acme:*:*:1");
    DependencyEmbedder dependencyEmbedder = new DependencyEmbedder(instructions, reporter);

    Artifact acme = create("acme", "acme-sample", "1");
    acme.setScope("provided");
    Artifact acme2 = create("acme", "acme-sample-2", "1");
    ProjectDependencies dependencies = new ProjectDependencies(
            ImmutableList.of(create("org.wisdom-framework", "wisdom-api", "1"),
                    acme, acme2),
            ImmutableList.of(create("org.wisdom-framework", "wisdom-api", "1"),
                    acme, acme2)
    );
    Properties properties = dependencyEmbedder.generate(instructions, dependencies);

    assertThat(properties.getProperty(Constants.INCLUDE_RESOURCE))
            .contains("@" + acme.getFile().getAbsolutePath())
            .contains("@" + acme2.getFile().getAbsolutePath());

    assertThat(properties.getProperty(Constants.BUNDLE_CLASSPATH)).isNull();

    instructions.setProperty(DependencyEmbedder.EMBED_DEPENDENCY, "acme:*:*:2");
    dependencyEmbedder = new DependencyEmbedder(instructions, reporter);

    acme = create("acme", "acme-sample", "2");
    acme.setScope("provided");
    acme2 = create("acme", "acme-sample-2", "1");
    dependencies = new ProjectDependencies(
            ImmutableList.of(create("org.wisdom-framework", "wisdom-api", "1"),
                    acme, acme2),
            ImmutableList.of(create("org.wisdom-framework", "wisdom-api", "1"),
                    acme, acme2)
    );
    properties = dependencyEmbedder.generate(instructions, dependencies);

    assertThat(properties.getProperty(Constants.INCLUDE_RESOURCE))
            .contains("@" + acme.getFile().getAbsolutePath())
            .doesNotContain("@" + acme2.getFile().getAbsolutePath());

    assertThat(properties.getProperty(Constants.BUNDLE_CLASSPATH)).isNull();
}