Java Code Examples for org.apache.maven.project.MavenProject#setArtifact()

The following examples show how to use org.apache.maven.project.MavenProject#setArtifact() . 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: AbstractSundrioMojo.java    From sundrio with Apache License 2.0 6 votes vote down vote up
MavenProject readProject(File pomFile) throws IOException {
    MavenXpp3Reader mavenReader = new MavenXpp3Reader();
    FileReader fileReader = null;
    try {
        fileReader = new FileReader(pomFile);
        Model model = mavenReader.read(fileReader);
        model.setPomFile(pomFile);
        MavenProject project = new MavenProject(model);
        project.setFile(pomFile);
        project.setArtifact(createArtifact(pomFile, model.getGroupId(), model.getArtifactId(), model.getVersion(), "compile", model.getPackaging(), ""));
        return project;
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (fileReader != null) {
            fileReader.close();
        }
    }
}
 
Example 2
Source File: AggregatingGraphFactoryTest.java    From depgraph-maven-plugin with Apache License 2.0 6 votes vote down vote up
private MavenProject createMavenProject(String artifactId) {
  MavenProject project = new MavenProject();
  project.setArtifactId(artifactId);
  // Make sure that we can modify the list later.
  project.setCollectedProjects(new ArrayList<>());

  DefaultArtifact artifact = new DefaultArtifact("groupId", artifactId, "version", "compile", "jar", "", null);
  project.setArtifact(artifact);

  RepositorySystemSession repositorySession = mock(RepositorySystemSession.class);
  ProjectBuildingRequest projectBuildingRequest = mock(ProjectBuildingRequest.class);
  when(projectBuildingRequest.getRepositorySession()).thenReturn(repositorySession);
  //noinspection deprecation
  project.setProjectBuildingRequest(projectBuildingRequest);

  return project;
}
 
Example 3
Source File: MavenUtilsTest.java    From wisdom with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetDefaultPropertiesOnProjectWithProperties() throws Exception {
    Model model = new Model();
    model.setPomFile(new File("target/test-classes/maven/test/minimal.xml"));
    MavenProject project = new MavenProject(model);
    project.setFile(new File("target/test-classes/maven/test/minimal.xml"));
    project.setArtifactId("acme");
    project.setGroupId("corp.acme");
    project.setVersion("1.0.0-SNAPSHOT");
    final ProjectArtifact artifact = new ProjectArtifact(project);
    project.setArtifact(artifact);
    Build build = new Build();
    build.setDirectory(new File(project.getBasedir(), "target").getAbsolutePath());
    build.setOutputDirectory(new File(project.getBasedir(), "target/classes").getAbsolutePath());
    project.setBuild(build);

    Properties props = new Properties();
    props.put("p", "v");
    model.setProperties(props);

    Properties properties = MavenUtils.getDefaultProperties(project);

    assertThat(properties.getProperty("p")).isEqualTo("v");
}
 
Example 4
Source File: A_ModuleGenerator.java    From deadcode4j with Apache License 2.0 6 votes vote down vote up
private MavenProject givenMavenProject(String projectId) {
    MavenProject mavenProject = new MavenProject();
    mavenProject.setGroupId("de.is24.junit");
    mavenProject.setArtifactId(projectId);
    mavenProject.setVersion("42");
    mavenProject.getProperties().setProperty("project.build.sourceEncoding", "UTF-8");
    ArtifactStub projectArtifact = new ArtifactStub();
    projectArtifact.setGroupId("de.is24.junit");
    projectArtifact.setArtifactId(projectId);
    projectArtifact.setVersion("42");
    mavenProject.setArtifact(projectArtifact);
    Build build = new Build();
    build.setOutputDirectory(tempFileRule.getTempFile().getParent());
    mavenProject.setBuild(build);
    return mavenProject;
}
 
Example 5
Source File: PackageApplicationMojoTest.java    From roboconf-platform with Apache License 2.0 5 votes vote down vote up
@Test( expected = MojoExecutionException.class )
public void testInvalidAppProject() throws Exception {

	final String finalName = "output";
	final String version = "1";

	// Copy the project
	File targetDirectory = this.resources.getBasedir( "project--valid" );
	File targetArchive = new File( targetDirectory, "target/" + finalName + ".zip" );
	File modelDirectory = new File( targetDirectory, MavenPluginConstants.TARGET_MODEL_DIRECTORY );
	Assert.assertFalse( targetArchive.exists());
	Assert.assertFalse( modelDirectory.exists());

	// Create the Maven project by hand
	File pom = new File( targetDirectory, "pom.xml" );
	final MavenProject mvnProject = new MavenProject() ;
	mvnProject.setFile( pom ) ;
	mvnProject.setVersion( version );
	mvnProject.getBuild().setDirectory( modelDirectory.getAbsolutePath());
	mvnProject.getBuild().setOutputDirectory( modelDirectory.getParentFile().getAbsolutePath());
	mvnProject.getBuild().setFinalName( finalName );
	mvnProject.setArtifact( new ProjectArtifact( mvnProject ));

	// Do NOT copy the resources

	// Package
	PackageApplicationMojo packageApplicationMojo = (PackageApplicationMojo) this.rule.lookupMojo( "package-application", pom );
	this.rule.setVariableValueToObject( packageApplicationMojo, "project", mvnProject );
	packageApplicationMojo.execute();
}
 
Example 6
Source File: MavenUtilsTest.java    From wisdom with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetDefaultPropertiesOnMinimalPom() throws Exception {
    Model model = new Model();
    model.setPomFile(new File("target/test-classes/maven/test/minimal.xml"));
    MavenProject project = new MavenProject(model);
    project.setFile(new File("target/test-classes/maven/test/minimal.xml"));
    project.setArtifactId("acme");
    project.setGroupId("corp.acme");
    project.setVersion("1.0.0-SNAPSHOT");
    final ProjectArtifact artifact = new ProjectArtifact(project);
    project.setArtifact(artifact);
    Build build = new Build();
    build.setDirectory(new File(project.getBasedir(), "target").getAbsolutePath());
    build.setOutputDirectory(new File(project.getBasedir(), "target/classes").getAbsolutePath());
    project.setBuild(build);

    Properties properties = MavenUtils.getDefaultProperties(project);
    assertThat(properties.getProperty("maven-symbolicname")).isEqualTo(DefaultMaven2OsgiConverter
            .getBundleSymbolicName(artifact));
    assertThat(properties.getProperty(org.osgi.framework.Constants.BUNDLE_SYMBOLICNAME)).isEqualTo(DefaultMaven2OsgiConverter
            .getBundleSymbolicName(artifact));

    assertThat(properties.getProperty(org.osgi.framework.Constants.BUNDLE_VERSION)).isEqualTo(DefaultMaven2OsgiConverter
            .getVersion(project.getVersion()));

    assertThat(properties.getProperty(org.osgi.framework.Constants.BUNDLE_DESCRIPTION)).isNull();
    assertThat(properties.getProperty(Analyzer.BUNDLE_LICENSE)).isNull();
    assertThat(properties.getProperty(Analyzer.BUNDLE_VENDOR)).isNull();
    assertThat(properties.getProperty(Analyzer.BUNDLE_DOCURL)).isNull();

    assertThat(properties.getProperty(Analyzer.BUNDLE_LICENSE)).isNull();

    assertThat(properties.getProperty(Analyzer.BUNDLE_NAME)).isEqualTo(project.getArtifactId());
}
 
Example 7
Source File: PackageApplicationMojoTest.java    From roboconf-platform with Apache License 2.0 4 votes vote down vote up
@Test
public void testValidAppProject() throws Exception {

	final String finalName = "output";
	final String version = "1";

	// Copy the project
	File targetDirectory = this.resources.getBasedir( "project--valid" );
	File targetArchive = new File( targetDirectory, "target/" + finalName + ".zip" );
	File modelDirectory = new File( targetDirectory, MavenPluginConstants.TARGET_MODEL_DIRECTORY );
	Assert.assertFalse( targetArchive.exists());
	Assert.assertFalse( modelDirectory.exists());

	// Create the Maven project by hand
	File pom = new File( targetDirectory, "pom.xml" );
	final MavenProject mvnProject = new MavenProject() ;
	mvnProject.setFile( pom ) ;
	mvnProject.setVersion( version );
	mvnProject.getBuild().setDirectory( modelDirectory.getParentFile().getAbsolutePath());
	mvnProject.getBuild().setOutputDirectory( modelDirectory.getAbsolutePath());
	mvnProject.getBuild().setFinalName( finalName );
	mvnProject.setArtifact( new ProjectArtifact( mvnProject ));

	// Copy the resources - mimic what Maven would really do
	Utils.copyDirectory(
			new File( mvnProject.getBasedir(), MavenPluginConstants.SOURCE_MODEL_DIRECTORY ),
			new File( mvnProject.getBuild().getOutputDirectory()));

	// Package
	PackageApplicationMojo packageApplicationMojo = (PackageApplicationMojo) this.rule.lookupMojo( "package-application", pom );
	this.rule.setVariableValueToObject( packageApplicationMojo, "project", mvnProject );
	packageApplicationMojo.execute();

	// Check assertions.
	// Unfortunately, no filtering here.
	Assert.assertTrue( targetArchive.exists());
	targetDirectory = this.folder.newFolder();
	Utils.extractZipArchive( targetArchive, targetDirectory );

	ApplicationLoadResult alr = RuntimeModelIo.loadApplication( targetDirectory );
	Assert.assertEquals( 0, alr.getLoadErrors().size());
	Assert.assertEquals( "1.0.0", alr.getApplicationTemplate().getVersion());

	File notFilteredFile = new File( targetDirectory, "graph/Tomcat/readme.md" );
	Assert.assertTrue( notFilteredFile.exists());

	String content = Utils.readFileContent( notFilteredFile );
	Assert.assertTrue( content.contains( "${project.version}" ));
	Assert.assertFalse( content.contains( "1.0-SNAPSHOT" ));
}
 
Example 8
Source File: PackageTargetMojoTest.java    From roboconf-platform with Apache License 2.0 4 votes vote down vote up
@Test
public void testValidTargetProject() throws Exception {

	final String finalName = "output";
	final String version = "1";

	// Copy the project
	File baseDirectory = this.resources.getBasedir( "target-ok-multi" );
	File compileDirectory = new File( baseDirectory, "target/test/classes" );
	File targetArchive = new File( baseDirectory, "target/" + finalName + ".zip" );
	Assert.assertFalse( targetArchive.exists());

	// Create the Maven project by hand
	File pom = new File( baseDirectory, "pom.xml" );
	final MavenProject mvnProject = new MavenProject() ;
	mvnProject.setFile( pom ) ;
	mvnProject.setVersion( version );
	mvnProject.getBuild().setFinalName( finalName );
	mvnProject.setArtifact( new ProjectArtifact( mvnProject ));
	mvnProject.getBuild().setDirectory( new File( baseDirectory, "target" ).getAbsolutePath());
	mvnProject.getBuild().setOutputDirectory( compileDirectory.getAbsolutePath());

	// Copy the resources - mimic what Maven would really do
	Utils.copyDirectory(
			new File( mvnProject.getBasedir(), "src/main/resources" ),
			new File( mvnProject.getBuild().getOutputDirectory()));

	// Package
	PackageTargetMojo packageTargetMojo = (PackageTargetMojo) this.rule.lookupMojo( "package-target", pom );
	this.rule.setVariableValueToObject( packageTargetMojo, "project", mvnProject );
	packageTargetMojo.execute();

	// Check assertions.
	// Unfortunately, no filtering here.
	Assert.assertTrue( targetArchive.exists());
	File unzipDirectory = this.folder.newFolder();
	Utils.extractZipArchive( targetArchive, unzipDirectory );

	List<File> files = Utils.listAllFiles( unzipDirectory );
	Assert.assertEquals( 2, files.size());
	Assert.assertTrue( files.contains( new File( unzipDirectory, "test1.properties" )));
	Assert.assertTrue( files.contains( new File( unzipDirectory, "test2.properties" )));
}
 
Example 9
Source File: MavenUtilsTest.java    From wisdom with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetDefaultPropertiesOnProjectWithLicenses() throws Exception {
    Model model = new Model();
    model.setPomFile(new File("target/test-classes/maven/test/minimal.xml"));
    MavenProject project = new MavenProject(model);
    project.setFile(new File("target/test-classes/maven/test/minimal.xml"));
    project.setArtifactId("acme");
    project.setGroupId("corp.acme");
    project.setVersion("1.0.0-SNAPSHOT");
    final ProjectArtifact artifact = new ProjectArtifact(project);
    project.setArtifact(artifact);
    Build build = new Build();
    build.setDirectory(new File(project.getBasedir(), "target").getAbsolutePath());
    build.setOutputDirectory(new File(project.getBasedir(), "target/classes").getAbsolutePath());
    project.setBuild(build);

    License license = new License();
    license.setDistribution("repo");
    license.setName("Apache Software License 2.0");
    license.setUrl("http://www.apache.org/licenses/");
    project.setLicenses(ImmutableList.of(license));

    Organization organization = new Organization();
    organization.setName("Acme Corp.");
    organization.setUrl("http://acme.org");
    project.setOrganization(organization);

    project.setDescription("description");

    Properties properties = MavenUtils.getDefaultProperties(project);
    assertThat(properties.getProperty(Analyzer.BUNDLE_LICENSE)).contains(license.getUrl());
    assertThat(properties.getProperty(Analyzer.BUNDLE_VENDOR)).isEqualTo("Acme Corp.");
    assertThat(properties.getProperty(Analyzer.BUNDLE_DOCURL)).isEqualTo(organization.getUrl());
    assertThat(properties.getProperty(Analyzer.BUNDLE_DESCRIPTION)).isEqualTo("description");

    License license2 = new License();
    license2.setDistribution("repo");
    license2.setName("Apache Software License 2.0");
    license2.setUrl("http://www.apache.org/LICENSE.txt");

    project.setLicenses(ImmutableList.of(license, license2));

    properties = MavenUtils.getDefaultProperties(project);
    assertThat(properties.getProperty(Analyzer.BUNDLE_LICENSE)).contains(license.getUrl()).contains(license2.getUrl());
}