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

The following examples show how to use org.apache.maven.project.MavenProject#setFile() . 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: MavenUtilsTest.java    From wisdom with Apache License 2.0 6 votes vote down vote up
@Test
public void testDumpEmptyDependencies() 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"));

    Classpath.store(project);

    File deps = new File(project.getBasedir(), Constants.DEPENDENCIES_FILE);
    assertThat(deps).isFile();
    ObjectMapper mapper = new ObjectMapper();
    ProjectDependencies dependencies = mapper.readValue(deps, ProjectDependencies.class);
    System.out.println(dependencies.getDirectDependencies());

    assertThat(Classpath.load(project.getBasedir()).getDirectDependencies()).isEmpty();
    assertThat(Classpath.load(project.getBasedir()).getTransitiveDependencies()).isEmpty();
}
 
Example 2
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 3
Source File: MavenEmbedder.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * normalize all File references in the object tree.
 * @param project 
 * @since 2.36
 */
public static void normalizePaths(MavenProject project) {
    if (project == null) {
        return;
    }
    File f = project.getFile();
    if (f != null) {
        project.setFile(FileUtil.normalizeFile(f));
    }
    normalizePath(project.getArtifact());
    normalizePaths(project.getAttachedArtifacts());
    f = project.getParentFile();
    if (f != null) {
        project.setParentFile(FileUtil.normalizeFile(f));
    }
    normalizePath(project.getParentArtifact());
    
    normalizePaths(project.getArtifacts());
    normalizePaths(project.getDependencyArtifacts());
    normalizePaths(project.getExtensionArtifacts());
    normalizePaths(project.getPluginArtifacts());
    
    normalizePath(project.getModel());
    normalizePath(project.getOriginalModel());
}
 
Example 4
Source File: AbstractTest.java    From roboconf-platform with Apache License 2.0 6 votes vote down vote up
protected AbstractMojo findMojo( String projectName, String goalName ) throws Exception {

		// Find the project
		File baseDir = this.resources.getBasedir( projectName );
		Assert.assertNotNull( baseDir );
		Assert.assertTrue( baseDir.isDirectory());

		File pom = new File( baseDir, "pom.xml" );
		AbstractMojo mojo = (AbstractMojo) this.rule.lookupMojo( goalName, pom );
		Assert.assertNotNull( mojo );

		// Create the Maven project by hand (...)
		final MavenProject mvnProject = new MavenProject() ;
		mvnProject.setFile( pom ) ;

		this.rule.setVariableValueToObject( mojo, "project", mvnProject );
		Assert.assertNotNull( this.rule.getVariableValueFromObject( mojo, "project" ));

		// Initialize the project
		InitializeMojo initMojo = new InitializeMojo();
		initMojo.setProject( mvnProject );
		initMojo.execute();

		return mojo;
	}
 
Example 5
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 6
Source File: MavenPluginHelper.java    From helidon-build-tools with Apache License 2.0 5 votes vote down vote up
public MavenProject newMavenProject(String pom, File dir)
        throws IOException {

    File pomFile = getTestFile("src/test/resources/" + pom);
    org.apache.maven.model.Model model = new DefaultModelReader()
            .read(pomFile, Collections.emptyMap());
    model.getBuild().setDirectory(dir.getAbsolutePath());
    MavenProject project = new MavenProject(model);
    project.getProperties().put("project.build.sourceEncoding", "UTF-8");
    project.setFile(pomFile);
    return project;
}
 
Example 7
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 8
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 9
Source File: DockerAssemblyConfigurationSourceTest.java    From docker-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void testReactorProjects() {
	MavenProject reactorProject1 = new MavenProject();
	reactorProject1.setFile(new File("../reactor-1"));

    MavenProject reactorProject2 = new MavenProject();
    reactorProject2.setFile(new File("../reactor-2"));

    DockerAssemblyConfigurationSource source = new DockerAssemblyConfigurationSource(
           new MojoParameters(null, null, null, null, null, null, "/src/docker", "/output/docker", Arrays.asList(new MavenProject[] { reactorProject1, reactorProject2 })),
           null,null
    );
    assertEquals(2,source.getReactorProjects().size());
}
 
Example 10
Source File: MavenSessionMock.java    From gitflow-incremental-builder with MIT License 5 votes vote down vote up
private static MavenProject createProject(Path path) {
    MavenProject project = new MavenProject();
    Model model = new Model();
    model.setProperties(new Properties());
    project.setModel(model);
    project.setArtifactId(path.getFileName().toString());
    project.setGroupId(path.getFileName().toString());
    project.setVersion("1");
    project.setFile(path.resolve("pom.xml").toFile());
    return project;
}
 
Example 11
Source File: MavenProjectCache.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@NbBundle.Messages({
    "LBL_Incomplete_Project_Name=<partially loaded Maven project>",
    "LBL_Incomplete_Project_Desc=Partially loaded Maven project; try building it."
})
public static MavenProject getFallbackProject(File projectFile) throws AssertionError {
    MavenProject newproject = new MavenProject();
    newproject.setGroupId("error");
    newproject.setArtifactId("error");
    newproject.setVersion("0");
    newproject.setPackaging("pom");
    newproject.setName(Bundle.LBL_Incomplete_Project_Name());
    newproject.setDescription(Bundle.LBL_Incomplete_Project_Desc());
    newproject.setFile(projectFile);
    return newproject;
}
 
Example 12
Source File: MavenPluginHelper.java    From helidon-build-tools with Apache License 2.0 5 votes vote down vote up
public MavenProject newMavenProject(String pom, File dir)
        throws IOException {

    File pomFile = getTestFile("src/test/resources/" + pom);
    org.apache.maven.model.Model model = new DefaultModelReader()
            .read(pomFile, Collections.emptyMap());
    model.getBuild().setDirectory(dir.getAbsolutePath());
    MavenProject project = new MavenProject(model);
    project.getProperties().put("project.build.sourceEncoding", "UTF-8");
    project.setFile(pomFile);
    return project;
}
 
Example 13
Source File: BomGeneratorMojo.java    From camel-spring-boot with Apache License 2.0 5 votes vote down vote up
private MavenProject loadExternalProjectPom(File pomFile) throws Exception {
    try (FileReader reader = new FileReader(pomFile)) {
        MavenXpp3Reader mavenReader = new MavenXpp3Reader();
        Model model = mavenReader.read(reader);

        MavenProject project = new MavenProject(model);
        project.setFile(pomFile);
        return project;
    }
}
 
Example 14
Source File: DockerAssemblyConfigurationSourceTest.java    From docker-maven-plugin with Apache License 2.0 4 votes vote down vote up
private MojoParameters buildParameters(String projectDir, String sourceDir, String outputDir) {
    MavenProject mavenProject = new MavenProject();
    mavenProject.setFile(new File(projectDir));
    return new MojoParameters(null, mavenProject, null, null, null, null, sourceDir, outputDir, null);
}
 
Example 15
Source File: ExternalVersionExtension.java    From maven-external-version with Apache License 2.0 4 votes vote down vote up
private void createNewVersionPom( MavenProject mavenProject, Map<String, String> gavVersionMap )
    throws IOException, XmlPullParserException
{
    Reader fileReader = null;
    Writer fileWriter = null;
    try
    {
        fileReader = new FileReader( mavenProject.getFile() );
        Model model = new MavenXpp3Reader().read( fileReader );
        model.setVersion( mavenProject.getVersion() );


        // TODO: this needs to be restructured when other references are updated (dependencies, dep-management, plugins, etc)
        if ( model.getParent() != null )
        {
            String key = buildGavKey( model.getParent().getGroupId(), model.getParent().getArtifactId(),
                                      model.getParent().getVersion() );
            String newVersionForParent = gavVersionMap.get( key );
            if ( newVersionForParent != null )
            {
                model.getParent().setVersion( newVersionForParent );
            }
        }
        
        Plugin plugin = mavenProject.getPlugin( "org.apache.maven.plugins:maven-external-version-plugin" );
        // now we are going to wedge in the config
        Xpp3Dom pluginConfiguration = (Xpp3Dom) plugin.getConfiguration();
        
        File newPom = createFileFromConfiguration( mavenProject, pluginConfiguration ); 
        logger.debug( ExternalVersionExtension.class.getSimpleName() + ": using new pom file => " + newPom );
        fileWriter = new FileWriter( newPom );
        new MavenXpp3Writer().write( fileWriter, model );

        mavenProject.setFile( newPom );
    }
    finally
    {
        IOUtil.close( fileReader );
        IOUtil.close( fileWriter );
    }


}
 
Example 16
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 17
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 18
Source File: CassandraServer.java    From geowave with Apache License 2.0 4 votes vote down vote up
private StartGeoWaveCluster(final int numNodes, final int memory, final String directory) {
  super();
  startWaitSeconds = 180;
  rpcAddress = "127.0.0.1";
  rpcPort = 9160;
  jmxPort = 7199;
  startNativeTransport = true;
  nativeTransportPort = 9042;
  listenAddress = "127.0.0.1";
  storagePort = 7000;
  stopPort = 8081;
  stopKey = "cassandra-maven-plugin";
  maxMemory = memory;
  cassandraDir = new File(directory, NODE_DIRECTORY_PREFIX);
  logLevel = "ERROR";
  project = new MavenProject();
  project.setFile(cassandraDir);
  Field f;
  try {
    f = StartCassandraClusterMojo.class.getDeclaredField("clusterSize");
    f.setAccessible(true);
    f.set(this, numNodes);
    f = AbstractCassandraMojo.class.getDeclaredField("pluginArtifact");

    f.setAccessible(true);
    final DefaultArtifact a =
        new DefaultArtifact(
            "group",
            "artifact",
            VersionRange.createFromVersionSpec("version"),
            null,
            "type",
            null,
            new DefaultArtifactHandler());
    a.setFile(cassandraDir);
    f.set(this, a);

    f = AbstractCassandraMojo.class.getDeclaredField("pluginDependencies");
    f.setAccessible(true);
    f.set(this, new ArrayList<>());
  } catch (NoSuchFieldException | SecurityException | IllegalArgumentException
      | IllegalAccessException | InvalidVersionSpecificationException e) {
    LOGGER.error("Unable to initialize start cassandra cluster", e);
  }
}
 
Example 19
Source File: CassandraServer.java    From geowave with Apache License 2.0 4 votes vote down vote up
private StartGeoWaveStandalone(final int memory, final String directory) {
  super();
  startWaitSeconds = 180;
  rpcAddress = "127.0.0.1";
  rpcPort = 9160;
  jmxPort = 7199;
  startNativeTransport = true;
  nativeTransportPort = 9042;
  listenAddress = "127.0.0.1";
  storagePort = 7000;
  stopPort = 8081;
  stopKey = "cassandra-maven-plugin";
  maxMemory = memory;
  cassandraDir = new File(directory);
  logLevel = "ERROR";
  project = new MavenProject();
  project.setFile(cassandraDir);
  Field f;
  try {
    f = AbstractCassandraMojo.class.getDeclaredField("pluginArtifact");

    f.setAccessible(true);
    final DefaultArtifact a =
        new DefaultArtifact(
            "group",
            "artifact",
            VersionRange.createFromVersionSpec("version"),
            null,
            "type",
            null,
            new DefaultArtifactHandler());
    a.setFile(cassandraDir);
    f.set(this, a);

    f = AbstractCassandraMojo.class.getDeclaredField("pluginDependencies");
    f.setAccessible(true);
    f.set(this, new ArrayList<>());
  } catch (NoSuchFieldException | SecurityException | IllegalArgumentException
      | IllegalAccessException | InvalidVersionSpecificationException e) {
    LOGGER.error("Unable to initialize start cassandra cluster", e);
  }
}
 
Example 20
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());
}