org.apache.maven.artifact.handler.DefaultArtifactHandler Java Examples

The following examples show how to use org.apache.maven.artifact.handler.DefaultArtifactHandler. 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: MavenUtilTest.java    From jkube with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testGetTransitiveDependencies(@Mocked MavenProject mavenProject) {
  // Given
  final Artifact artifact1 = new DefaultArtifact("org.eclipse.jkube", "foo-dependency", "1.33.7",
      "runtime", "jar", "", new DefaultArtifactHandler("jar"));
  final Artifact artifact2 = new DefaultArtifact("org.eclipse.jkube", "bar-dependency", "1.33.7",
      "runtime", "jar", "", new DefaultArtifactHandler("jar"));
  new Expectations() {{
    mavenProject.getArtifacts();
    result = new HashSet<>(Arrays.asList(artifact1, artifact2));
  }};
  // When
  final List<Dependency> result = MavenUtil.getTransitiveDependencies(mavenProject);
  // Then
  assertThat(result, hasSize(2));
  assertThat(result, contains(
      equalTo(Dependency.builder().groupId("org.eclipse.jkube").artifactId("foo-dependency").version("1.33.7")
          .type("jar").scope("runtime").build()),
      equalTo(Dependency.builder().groupId("org.eclipse.jkube").artifactId("bar-dependency").version("1.33.7")
          .type("jar").scope("runtime").build())
  ));
}
 
Example #2
Source File: NativeRanlibMojoTest.java    From maven-native with MIT License 6 votes vote down vote up
public void testMojoLookup()
    throws Exception
{
    File pluginXml = new File( getBasedir(), "src/test/resources/linker/plugin-config-ranlib.xml" );
    NativeRanlibMojo mojo = (NativeRanlibMojo) lookupMojo( "ranlib", pluginXml );
    assertNotNull( mojo );

    // simulate artifact
    ArtifactHandler artifactHandler = new DefaultArtifactHandler();

    Artifact artifact = new DefaultArtifact( "test", "test", VersionRange.createFromVersion( "1.0-SNAPSHOT" ),
            "compile", "exe", null, artifactHandler );
    mojo.getProject().setArtifact( artifact );

    mojo.execute();
}
 
Example #3
Source File: NativeInitializeMojoTest.java    From maven-native with MIT License 6 votes vote down vote up
public void testMojoLookup()
    throws Exception
{
    File pluginXml = new File( getBasedir(), "src/test/resources/initialize/plugin-config.xml" );
    NativeInitializeMojo mojo = (NativeInitializeMojo) lookupMojo( "initialize", pluginXml );
    assertNotNull( mojo );

    // simulate artifact
    ArtifactHandler artifactHandler = new DefaultArtifactHandler();
    Artifact artifact = new DefaultArtifact( "test", "test", VersionRange.createFromVersion( "1.0-SNAPSHOT" ),
            "compile", "exe", null, artifactHandler );
    mojo.project.setArtifact( artifact );
    mojo.setPluginContext( new HashMap<>() );

    mojo.execute();

    assertEquals( "someArtifactId", mojo.project.getBuild().getFinalName() );
}
 
Example #4
Source File: MavenUtils.java    From google-cloud-eclipse with Apache License 2.0 6 votes vote down vote up
/**
 * Checks if an artifact is available in the local repository. The artifact <code>version</code>
 * must be a specific value, cannot be "LATEST".
 */
public static boolean isArtifactAvailableLocally(String groupId, String artifactId,
                                                 String version, String type,
                                                 String classifier) {
  try {
    Preconditions.checkArgument(!MAVEN_LATEST_VERSION.equals(version));
    String artifactPath =
        MavenPlugin.getMaven().getLocalRepository()
            .pathOf(new DefaultArtifact(groupId, artifactId, version, null /* scope */, type,
                                        classifier, new DefaultArtifactHandler(type)));
    return new File(artifactPath).exists();
  } catch (CoreException ex) {
    logger.log(Level.SEVERE, "Could not lookup local repository", ex);
    return false;
  }
}
 
Example #5
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 #6
Source File: ProblemReporterImplTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testMissingParent() throws Exception {
    TestFileUtils.writeFile(new File(getWorkDir(), "pom.xml"), "<project xmlns='http://maven.apache.org/POM/4.0.0'><modelVersion>4.0.0</modelVersion>" +
        "<parent><groupId>g</groupId><artifactId>par</artifactId><version>0</version></parent>" +
        "<artifactId>m</artifactId>" +
        "</project>");
    Project p = ProjectManager.getDefault().findProject(FileUtil.toFileObject(getWorkDir()));
    assertEquals("g:m:jar:0", p.getLookup().lookup(NbMavenProject.class).getMavenProject().getId());
    ProblemReporterImpl pr = getReporter(p);
    MavenModelProblemsProvider mpp = new MavenModelProblemsProvider(p);
    Collection<? extends ProjectProblemsProvider.ProjectProblem> problems = mpp.getProblems();
    
    waitForReports();
    assertFalse(problems.isEmpty());
    
    assertEquals(Collections.singleton(a2f(new DefaultArtifact("g", "par", "0", null, "pom", null, new DefaultArtifactHandler("pom")))), pr.getMissingArtifactFiles());
}
 
Example #7
Source File: ArtifactVersionsTest.java    From versions-maven-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void testSmokes()
    throws Exception
{
    ArtifactVersion[] versions = versions( "1.0", "3.0", "1.1", "1.0", "1.0.1" );
    final DefaultArtifact artifact =
        new DefaultArtifact( "group", "artifact", VersionRange.createFromVersionSpec( "[1.0,3.0]" ), "foo", "bar",
                             "jar", new DefaultArtifactHandler() );
    ArtifactVersions instance =
        new ArtifactVersions( artifact, Arrays.asList( versions ), new MavenVersionComparator() );
    assertEquals( "artifact", instance.getArtifactId() );
    assertEquals( "group", instance.getGroupId() );
    assertArrayEquals(
        versions( "1.0", "1.0.1", "1.1", "3.0" ),
        instance.getVersions() );
    assertArrayEquals( versions( "3.0" ),
                       instance.getVersions( new DefaultArtifactVersion( "1.1" ), null ) );
    assertArrayEquals(
        versions( "1.1", "3.0" ),
        instance.getVersions( new DefaultArtifactVersion( "1.0.1" ), null ) );
    assertEquals( new DefaultArtifactVersion( "1.1" ),
                  instance.getNewestVersion( new DefaultArtifactVersion( "1.0" ),
                                             new DefaultArtifactVersion( "3.0" ) ) );
    assertNull(
        instance.getNewestVersion( new DefaultArtifactVersion( "1.1" ), new DefaultArtifactVersion( "3.0" ) ) );
}
 
Example #8
Source File: LinkageCheckerRuleTest.java    From cloud-opensource-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testValidatePhase() {
  when(mockProject.getArtifact())
      .thenReturn(
          new org.apache.maven.artifact.DefaultArtifact(
              "com.google.cloud",
              "foo-tests",
              "0.0.1",
              "compile",
              "jar",
              null,
              new DefaultArtifactHandler()));

  when(mockMojoExecution.getLifecyclePhase()).thenReturn("validate");
  try {
    rule.execute(mockRuleHelper);
    fail("The rule should throw EnforcerRuleException when running in validate phase");
  } catch (EnforcerRuleException ex) {
    assertEquals(
        "To run the check on the compiled class files, the linkage checker enforcer rule should"
            + " be bound to the 'verify' phase. Current phase: validate",
        ex.getMessage());
  }
}
 
Example #9
Source File: ForArtifactMojo.java    From depgraph-maven-plugin with Apache License 2.0 6 votes vote down vote up
@Override
public MavenProject getProject() {
  ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(this.session.getProjectBuildingRequest());
  buildingRequest.setRepositorySession(this.session.getRepositorySession());
  buildingRequest.setProject(null);
  buildingRequest.setResolveDependencies(true);
  buildingRequest.setActiveProfileIds(this.profiles);

  DefaultArtifact artifact = new DefaultArtifact(this.groupId, this.artifactId, this.version, SCOPE_COMPILE, this.type, this.classifier, new DefaultArtifactHandler());
  try {
    return this.projectBuilder.build(artifact, buildingRequest).getProject();
  } catch (ProjectBuildingException e) {
    throw new IllegalStateException("Error while creating Maven project from Artifact '" + artifact + "'.", e);
  }

}
 
Example #10
Source File: LinkageCheckerRuleTest.java    From cloud-opensource-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testExecute_shouldSkipBadBomWithNonPomPackaging() throws EnforcerRuleException {
  rule.setDependencySection(DependencySection.DEPENDENCY_MANAGEMENT);
  setupMockDependencyManagementSection(
      "com.google.api-client:google-api-client:1.27.0", "io.grpc:grpc-core:1.17.1");
  when(mockProject.getArtifact())
      .thenReturn(
          new org.apache.maven.artifact.DefaultArtifact(
              "com.google.cloud",
              "linkage-checker-rule-test-bom",
              "0.0.1",
              "compile",
              "jar", // BOM should have pom here
              null,
              new DefaultArtifactHandler()));
  rule.execute(mockRuleHelper);
}
 
Example #11
Source File: LinkageCheckerRuleTest.java    From cloud-opensource-java with Apache License 2.0 6 votes vote down vote up
private void setupMockDependencyManagementSection(String... coordinates) {
  org.apache.maven.artifact.DefaultArtifact bomArtifact =
      new org.apache.maven.artifact.DefaultArtifact(
          "com.google.dummy",
          "dummy-bom",
          "0.1",
          "compile",
          "pom",
          "",
          new DefaultArtifactHandler());
  when(mockProject.getArtifact()).thenReturn(bomArtifact);

  DependencyManagement mockDependencyManagement = mock(DependencyManagement.class);
  when(mockProject.getDependencyManagement()).thenReturn(mockDependencyManagement);

  ImmutableList<org.apache.maven.model.Dependency> bomMembers =
      Arrays.stream(coordinates)
          .map(DefaultArtifact::new)
          .map(artifact -> new Dependency(artifact, "compile"))
          .map(LinkageCheckerRuleTest::toDependency)
          .collect(toImmutableList());
  when(mockDependencyManagement.getDependencies()).thenReturn(bomMembers);

  when(mockMavenSession.getRepositorySession()).thenReturn(repositorySystemSession);
}
 
Example #12
Source File: MappingTrackArchiver.java    From docker-maven-plugin with Apache License 2.0 5 votes vote down vote up
private Artifact getArtifactFromPomProperties(String type, Properties pomProps) {
    return new DefaultArtifact(
            pomProps.getProperty("groupId"),
            pomProps.getProperty("artifactId"),
            pomProps.getProperty("version"),
            "runtime",
            type,
            pomProps.getProperty("classifier", ""),
            new DefaultArtifactHandler(type)
    );
}
 
Example #13
Source File: GeneratorTest.java    From webstart with MIT License 5 votes vote down vote up
@Override
public void setUp()
    throws Exception
{
    super.setUp();
    DefaultArtifactHandler artifactHandler = new DefaultArtifactHandler( "jar" );
    artifact1 =
        new DefaultArtifact( "groupId", "artifact1", VersionRange.createFromVersion( "1.0" ), "scope", "jar",
                             "classifier", artifactHandler );
    artifact1.setFile( new File( "artifact1-1.0.jar" ) );
    artifact2 =
        new DefaultArtifact( "groupId", "artifact2", VersionRange.createFromVersion( "1.5" ), null, "jar", "",
                             artifactHandler );
    artifact2.setFile( new File( "artifact2-1.5.jar" ) );

    // add a SNAPSHOT artifact, timestamped (from a remote maven repository)
    artifact3 =
            new DefaultArtifact( "groupId", "artifact3", VersionRange.createFromVersion( "1.5-SNAPSHOT" ), null, "jar", "",
                                 artifactHandler );
    artifact3.setVersion("1.5-15012014.121212-1");
    artifact3.setFile( new File( "artifact3-1.5-15012014.121212-1.jar" ) );

    // add a SNAPSHOT artifact, not timestamped (from a local build)
    artifact4 =
            new DefaultArtifact( "groupId", "artifact4", VersionRange.createFromVersion( "1.5-SNAPSHOT" ), null, "jar", "",
                                 artifactHandler );
    artifact4.setFile( new File( "artifact4-1.5-SNAPSHOT.jar" ) );

    artifacts = new ArrayList<>();

    artifacts.add( artifact1 );
    artifacts.add( artifact2 );
    artifacts.add( artifact3 );
    artifacts.add( artifact4 );
}
 
Example #14
Source File: ResolveMojoTest.java    From roboconf-platform with Apache License 2.0 5 votes vote down vote up
@Test( expected = MojoExecutionException.class )
public void testWithInvalidRoboconfDependencies() throws Exception {

	// Prepare the project
	final String projectName = "project--valid";

	File baseDir = this.resources.getBasedir( projectName );
	Assert.assertNotNull( baseDir );
	Assert.assertTrue( baseDir.isDirectory());

	AbstractMojo mojo = findMojo( projectName, "resolve" );
	this.rule.setVariableValueToObject( mojo, "repoSystem", newRepositorySystem());
	this.rule.setVariableValueToObject( mojo, "repositories", new ArrayList<RemoteRepository>( 0 ));

	// Add dependencies
	MavenProject project = (MavenProject) this.rule.getVariableValueFromObject( mojo, "project" );
	project.setDependencyArtifacts( new HashSet<Artifact> ());

	Artifact notRbcfArtifact1 = new DefaultArtifact( "net.roboconf", "roboconf-core", "0.2", "runtime", "jar", null, new DefaultArtifactHandler());
	project.getDependencyArtifacts().add( notRbcfArtifact1 );

	Artifact notRbcfArtifact2 = new DefaultArtifact( "net.roboconf", "roboconf-core", "0.2", "runtime", "jar", null, new DefaultArtifactHandler());
	notRbcfArtifact2.setFile( new File( "file that does not exist" ));
	project.getDependencyArtifacts().add( notRbcfArtifact2 );

	Artifact notRbcfArtifact3 = new DefaultArtifact( "net.roboconf", "roboconf-core", "0.2", "runtime", "jar", null, new DefaultArtifactHandler());
	File temp = this.folder.newFile( "toto.zip" );
	Assert.assertTrue( temp.exists());

	notRbcfArtifact3.setFile( temp );
	project.getDependencyArtifacts().add( notRbcfArtifact3 );

	// Execute it
	File targetDir = new File( baseDir, MavenPluginConstants.TARGET_MODEL_DIRECTORY + "/" + Constants.PROJECT_DIR_GRAPH );
	Assert.assertFalse( targetDir.isDirectory());
	mojo.execute();
}
 
Example #15
Source File: ArtifactVersionsTest.java    From versions-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void test4DigitVersions() throws Exception {
    ArtifactVersion[] versions = versions( "1.0.0.1", "1.0.0.2", "2.121.2.1", "2.100.0.1", "3.1.0.1", "1.1.1");
    final DefaultArtifact artifact =
            new DefaultArtifact( "group", "artifact", VersionRange.createFromVersionSpec( "[1.0,3.0]" ), "foo", "bar",
                                 "jar", new DefaultArtifactHandler() );
    // TODO This should also work for the MavenVersionComparator when using maven 3.x libraries
    ArtifactVersions instance =
            new ArtifactVersions(artifact, Arrays.asList( versions ), new MercuryVersionComparator() );
    assertEquals( "artifact", instance.getArtifactId() );
    assertEquals( "group", instance.getGroupId() );
    assertThat(instance.getVersions(),
               Matchers.arrayContaining(versions( "1.0.0.1", "1.0.0.2", "1.1.1", "2.100.0.1", "2.121.2.1",
                                                  "3.1.0.1" )));
    assertThat(instance.getVersions( new DefaultArtifactVersion( "1.1" ), null ),
               Matchers.arrayContaining(versions("1.1.1", "2.100.0.1", "2.121.2.1", "3.1.0.1")));

    assertThat(instance.getVersions( new DefaultArtifactVersion( "1.0.0.2" ), null ),
                      //Matchers.arrayContaining(versions("1.1.1", "2.121.2.1", "2.100.0.1", "3.1.0.1")));
               Matchers.arrayContaining(versions("1.1.1", "2.100.0.1", "2.121.2.1", "3.1.0.1")));

    assertEquals( new DefaultArtifactVersion( "2.121.2.1" ),
                  instance.getNewestVersion( new DefaultArtifactVersion( "1.0" ),
                                             new DefaultArtifactVersion( "3.0" ) ) );
    assertNull(
            instance.getNewestVersion( new DefaultArtifactVersion( "1.1.1" ),
                                       new DefaultArtifactVersion( "2.0" ) ) );
}
 
Example #16
Source File: NativeLinkerMojoTest.java    From maven-native with MIT License 5 votes vote down vote up
public void testExecuteWithFinalNameExtension()
    throws Exception
{
    NativeLinkMojo mojo = getMojo();

    // simulate object files
    List<File> objectList = new ArrayList<>();
    objectList.add( new File( "o1.o" ) );
    objectList.add( new File( "o2.o" ) );
    mojo.saveCompilerOutputFilePaths( objectList );

    // simulate artifact
    ArtifactHandler artifactHandler = new DefaultArtifactHandler();

    Artifact artifact = new DefaultArtifact( "test", "test", VersionRange.createFromVersion( "1.0-SNAPSHOT" ),
            "compile", "exe", null, artifactHandler );
    mojo.getProject().setArtifact( artifact );

    // simulate artifacts
    mojo.getProject().setArtifacts( new HashSet<Artifact>() ); // no extern libs for now

    String linkerFinalName = "some-final-name";
    setVariableValueToObject( mojo, "linkerFinalName", linkerFinalName );
    String linkerFinalNameExt = "some-extension";
    setVariableValueToObject( mojo, "linkerFinalNameExt", linkerFinalNameExt );

    mojo.execute();

    LinkerConfiguration conf = mojo.getLgetLinkerConfiguration();

    // "target is set in the stub
    assertEquals( new File( "target" ), conf.getOutputDirectory() );
    assertEquals( linkerFinalName, conf.getOutputFileName() );
    assertEquals( linkerFinalNameExt, conf.getOutputFileExtension() );
    assertEquals( new File( "target/some-final-name.some-extension" ), conf.getOutputFile() );

}
 
Example #17
Source File: NativeLinkerMojoTest.java    From maven-native with MIT License 5 votes vote down vote up
public void testExecute()
    throws Exception
{
    NativeLinkMojo mojo = getMojo();

    // simulate object files
    List<File> objectList = new ArrayList<>();
    objectList.add( new File( "o1.o" ) );
    objectList.add( new File( "o2.o" ) );
    mojo.saveCompilerOutputFilePaths( objectList );

    // simulate artifact
    ArtifactHandler artifactHandler = new DefaultArtifactHandler();

    Artifact artifact = new DefaultArtifact( "test", "test", VersionRange.createFromVersion( "1.0-SNAPSHOT" ),
            "compile", "exe", null, artifactHandler );
    mojo.getProject().setArtifact( artifact );

    // simulate artifacts
    mojo.getProject().setArtifacts( new HashSet<Artifact>() ); // no extern libs for now

    String linkerFinalName = "some-final-name";
    setVariableValueToObject( mojo, "linkerFinalName", linkerFinalName );

    mojo.execute();

    LinkerConfiguration conf = mojo.getLgetLinkerConfiguration();

    // "target is set in the stub
    assertEquals( new File( "target" ), conf.getOutputDirectory() );
    assertEquals( linkerFinalName, conf.getOutputFileName() );
    assertNull( conf.getOutputFileExtension() );
    // current artifactHandler mocking return null extension name
    assertEquals( new File( "target/some-final-name.null" ), conf.getOutputFile() );

}
 
Example #18
Source File: AetherUtils.java    From takari-lifecycle with Eclipse Public License 1.0 5 votes vote down vote up
public static ArtifactHandler newHandler(Artifact artifact) {
  String type = artifact.getProperty(ArtifactProperties.TYPE, artifact.getExtension());
  DefaultArtifactHandler handler = new DefaultArtifactHandler(type);
  handler.setExtension(artifact.getExtension());
  handler.setLanguage(artifact.getProperty(ArtifactProperties.LANGUAGE, null));
  handler.setAddedToClasspath(Boolean.parseBoolean(artifact.getProperty(ArtifactProperties.CONSTITUTES_BUILD_PATH, "")));
  handler.setIncludesDependencies(Boolean.parseBoolean(artifact.getProperty(ArtifactProperties.INCLUDES_DEPENDENCIES, "")));
  return handler;
}
 
Example #19
Source File: TestPropertiesMojoTest.java    From takari-lifecycle with Eclipse Public License 1.0 5 votes vote down vote up
private MojoExecution newMojoExecution(Xpp3Dom... parameters) throws IOException {
  MojoExecution execution = mojos.newMojoExecution("testProperties", parameters);
  PluginDescriptor pluginDescriptor = execution.getMojoDescriptor().getPluginDescriptor();

  ArtifactHandler handler = new DefaultArtifactHandler("jar");
  DefaultArtifact workspaceResolver = new DefaultArtifact("io.takari.m2e.workspace", "org.eclipse.m2e.workspace.cli", "1", Artifact.SCOPE_COMPILE, ".jar", null, handler);
  workspaceResolver.setFile(new File("target/workspaceResolver.jar").getCanonicalFile());

  List<Artifact> pluginArtifacts = new ArrayList<>(pluginDescriptor.getArtifacts());
  pluginArtifacts.add(workspaceResolver);
  pluginDescriptor.setArtifacts(pluginArtifacts);

  return execution;
}
 
Example #20
Source File: PackageMojo.java    From vertx-maven-plugin with Apache License 2.0 5 votes vote down vote up
private void attachIfNeeded(File jar) {
    if (jar.isFile() && classifier != null && attach) {
        ArtifactHandler handler = new DefaultArtifactHandler("jar");
        Artifact vertxJarArtifact = new DefaultArtifact(project.getGroupId(),
            project.getArtifactId(), project.getVersion(), "compile",
            "jar", classifier, handler);
        vertxJarArtifact.setFile(jar);
        this.project.addAttachedArtifact(vertxJarArtifact);
    }
}
 
Example #21
Source File: TestProvidedArtifactsTest.java    From vespa with Apache License 2.0 5 votes vote down vote up
private static Artifact createArtifact(String artifactId, String... dependents) {
    Artifact artifact = new DefaultArtifact(GROUP_ID, artifactId, "1.0", "test", "jar", "deploy", new DefaultArtifactHandler("jar"));
    List<String> dependencyTrail = new ArrayList<>();
    dependencyTrail.add(fullId("bundle-plugin"));
    Arrays.stream(dependents).forEach(dependent -> dependencyTrail.add(fullId(dependent)));
    dependencyTrail.add(fullId(artifactId));
    artifact.setDependencyTrail(dependencyTrail);
    return artifact;
}
 
Example #22
Source File: ProblemReporterImplTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testMissingDependency() throws Exception {
    TestFileUtils.writeFile(new File(getWorkDir(), "pom.xml"), "<project xmlns='http://maven.apache.org/POM/4.0.0'><modelVersion>4.0.0</modelVersion>" +
        "<groupId>g</groupId><artifactId>m</artifactId><version>0</version>" +
        "<dependencies><dependency><groupId>g</groupId><artifactId>b</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies>" +
        "</project>");
    Project p = ProjectManager.getDefault().findProject(FileUtil.toFileObject(getWorkDir()));
    ProblemReporterImpl pr = getReporter(p);
    MavenModelProblemsProvider mpp = new MavenModelProblemsProvider(p);
    Collection<? extends ProjectProblemsProvider.ProjectProblem> problems = mpp.getProblems();
    waitForReports();
    assertFalse(problems.isEmpty());
    assertEquals(Collections.singleton(a2f(new DefaultArtifact("g", "b", "1.0-SNAPSHOT", "compile", "jar", null, new DefaultArtifactHandler("jar")))), pr.getMissingArtifactFiles());
}
 
Example #23
Source File: ProblemReporterImplTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testMissingPlugin() throws Exception {
    TestFileUtils.writeFile(new File(getWorkDir(), "pom.xml"), "<project xmlns='http://maven.apache.org/POM/4.0.0'><modelVersion>4.0.0</modelVersion>" +
        "<groupId>g</groupId><artifactId>m</artifactId><version>0</version>" +
        "<build><plugins><plugin><groupId>g</groupId><artifactId>plug</artifactId><version>0</version><extensions>true</extensions></plugin></plugins></build>" +
        "</project>");
    Project p = ProjectManager.getDefault().findProject(FileUtil.toFileObject(getWorkDir()));
    ProblemReporterImpl pr = getReporter(p);
    MavenModelProblemsProvider mpp = new MavenModelProblemsProvider(p);
    Collection<? extends ProjectProblemsProvider.ProjectProblem> problems = mpp.getProblems();
    waitForReports();
    assertFalse(problems.isEmpty());
    assertEquals(Collections.singleton(a2f(new DefaultArtifact("g", "plug", "0", null, "jar", null, new DefaultArtifactHandler("jar")))), pr.getMissingArtifactFiles());
}
 
Example #24
Source File: LinkageCheckerRuleTest.java    From cloud-opensource-java with Apache License 2.0 5 votes vote down vote up
private void setupMock()
    throws ExpressionEvaluationException, ComponentLookupException,
    DependencyResolutionException {
  mockProject = mock(MavenProject.class);
  mockMavenSession = mock(MavenSession.class);
  when(mockMavenSession.getRepositorySession()).thenReturn(repositorySystemSession);
  mockRuleHelper = mock(EnforcerRuleHelper.class);
  mockProjectDependenciesResolver = mock(ProjectDependenciesResolver.class);
  mockDependencyResolutionResult = mock(DependencyResolutionResult.class);
  mockLog = mock(Log.class);
  when(mockRuleHelper.getLog()).thenReturn(mockLog);
  when(mockRuleHelper.getComponent(ProjectDependenciesResolver.class))
      .thenReturn(mockProjectDependenciesResolver);
  when(mockProjectDependenciesResolver.resolve(any(DependencyResolutionRequest.class)))
      .thenReturn(mockDependencyResolutionResult);
  when(mockRuleHelper.evaluate("${session}")).thenReturn(mockMavenSession);
  when(mockRuleHelper.evaluate("${project}")).thenReturn(mockProject);
  mockMojoExecution = mock(MojoExecution.class);
  when(mockMojoExecution.getLifecyclePhase()).thenReturn("verify");
  when(mockRuleHelper.evaluate("${mojoExecution}")).thenReturn(mockMojoExecution);
  org.apache.maven.artifact.DefaultArtifact rootArtifact =
      new org.apache.maven.artifact.DefaultArtifact(
          "com.google.cloud",
          "linkage-checker-rule-test",
          "0.0.1",
          "compile",
          "jar",
          null,
          new DefaultArtifactHandler());
  rootArtifact.setFile(new File("dummy.jar"));
  when(mockProject.getArtifact()).thenReturn(rootArtifact);
}
 
Example #25
Source File: TreeMojoTestBase.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws Exception {

    final AbstractTreeMojo mojo = newTreeMojo();
    mojo.project = new MavenProject();
    mojo.project.setArtifact(new DefaultArtifact(app.getGroupId(), app.getArtifactId(), app.getVersion(), "compile",
            app.getType(), app.getClassifier(), new DefaultArtifactHandler("jar")));
    mojo.project.setModel(appModel);
    mojo.project.setOriginalModel(appModel);

    mojo.repoSystem = mvnResolver.getSystem();
    mojo.repoSession = mvnResolver.getSession();
    mojo.repos = mvnResolver.getRepositories();

    final Path mojoLog = workDir.resolve("mojo.log");
    final PrintStream defaultOut = System.out;

    try (PrintStream logOut = new PrintStream(mojoLog.toFile(), "UTF-8")) {
        System.setOut(logOut);
        mojo.execute();
    } finally {
        System.setOut(defaultOut);
    }

    assertEquals(readInLowCase(Paths.get("").toAbsolutePath().resolve("target").resolve("test-classes")
            .resolve(app.getArtifactFileName() + "." + mojoName())), readInLowCase(mojoLog));
}
 
Example #26
Source File: LinkageCheckerRuleTest.java    From cloud-opensource-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testSkippingProjectWithoutFile() throws EnforcerRuleException {
  when(mockProject.getArtifact())
      .thenReturn(
          new org.apache.maven.artifact.DefaultArtifact(
              "com.google.cloud",
              "foo-tests",
              "0.0.1",
              "compile",
              "jar",
              null,
              new DefaultArtifactHandler()));
  rule.execute(mockRuleHelper);
}
 
Example #27
Source File: LinkageCheckerRuleTest.java    From cloud-opensource-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testExecute_shouldFailForBadProjectWithBundlePackaging() throws RepositoryException {
  try {
    // This artifact is known to contain classes missing dependencies
    setupMockDependencyResolution("com.google.appengine:appengine-api-1.0-sdk:1.9.64");

    org.apache.maven.artifact.DefaultArtifact rootArtifact =
        new org.apache.maven.artifact.DefaultArtifact(
            "com.google.cloud",
            "linkage-checker-rule-test",
            "0.0.1",
            "compile",
            "bundle", // Maven Bundle Plugin uses "bundle" packaging.
            null,
            new DefaultArtifactHandler());
    rootArtifact.setFile(new File("dummy.jar"));
    when(mockProject.getArtifact()).thenReturn(rootArtifact);

    rule.execute(mockRuleHelper);
    Assert.fail(
        "The rule should raise an EnforcerRuleException for artifacts missing dependencies");
  } catch (EnforcerRuleException ex) {
    // pass
    verify(mockLog).error(ArgumentMatchers.startsWith("Linkage Checker rule found 112 errors."));
    assertEquals("Failed while checking class path. See above error report.", ex.getMessage());
  }
}
 
Example #28
Source File: LinkageCheckerRuleTest.java    From cloud-opensource-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testExecute_shouldSkipNonBomPom() throws EnforcerRuleException {
  when(mockProject.getArtifact())
      .thenReturn(
          new org.apache.maven.artifact.DefaultArtifact(
              "com.google.cloud",
              "linkage-checker-rule-parent",
              "0.0.1",
              "compile",
              "pom",
              null,
              new DefaultArtifactHandler()));
  // No exception
  rule.execute(mockRuleHelper);
}
 
Example #29
Source File: ArtifactDeploymentTest.java    From wildfly-maven-plugin with GNU Lesser General Public License v2.1 4 votes vote down vote up
private Artifact createArtifact(final String classifier) {
    final Artifact artifact = new DefaultArtifact("dummy", "dummy", "1.0.0", "provided", "jar", classifier, new DefaultArtifactHandler());
    artifact.setFile(new File(BASE_CONFIG_DIR, artifactName));
    return artifact;
}
 
Example #30
Source File: Classpath.java    From wisdom with Apache License 2.0 4 votes vote down vote up
@Override
public ArtifactHandler deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    return mapper.readValue(jp, DefaultArtifactHandler.class);
}