下面列出了怎么用org.apache.maven.project.artifact.ProjectArtifact的API类实例代码及写法,或者点击链接到github查看源代码。
public MavenProjectWrapper(Project project, File pomFile) throws IOException, XmlPullParserException {
this.project = project;
this.pomFile = pomFile;
MavenXpp3Reader reader = new MavenXpp3Reader();
Model model = reader.read(new FileReader(pomFile));
setModel(model);
getBuild().setDirectory(project.getBuildDir().getAbsolutePath());
SourceSetContainer sourceSets = project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets();
SourceSet main = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME);
getBuild().setSourceDirectory(main.getJava().getSrcDirs().iterator().next().getAbsolutePath());
getBuild().setOutputDirectory(main.getJava().getOutputDir().getAbsolutePath());
SourceSet test = sourceSets.getByName(SourceSet.TEST_SOURCE_SET_NAME);
getBuild().setTestSourceDirectory(test.getJava().getSrcDirs().iterator().next().getAbsolutePath());
getBuild().setTestOutputDirectory(test.getJava().getOutputDir().getAbsolutePath());
setArtifact(new ProjectArtifact(this));
}
public MavenProjectWrapper(Project project, File pomFile) throws IOException, XmlPullParserException {
this.project = project;
this.pomFile = pomFile;
MavenXpp3Reader reader = new MavenXpp3Reader();
Model model = reader.read(new FileReader(pomFile));
setModel(model);
getBuild().setDirectory(project.getBuildDir().getAbsolutePath());
SourceSetContainer sourceSets = project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets();
SourceSet main = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME);
getBuild().setSourceDirectory(main.getJava().getSrcDirs().iterator().next().getAbsolutePath());
getBuild().setOutputDirectory(main.getJava().getOutputDir().getAbsolutePath());
SourceSet test = sourceSets.getByName(SourceSet.TEST_SOURCE_SET_NAME);
getBuild().setTestSourceDirectory(test.getJava().getSrcDirs().iterator().next().getAbsolutePath());
getBuild().setTestOutputDirectory(test.getJava().getOutputDir().getAbsolutePath());
setArtifact(new ProjectArtifact(this));
}
@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");
}
@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();
}
@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());
}
@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" ));
}
@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" )));
}
@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());
}