Java Code Examples for org.apache.maven.model.Model#setVersion()

The following examples show how to use org.apache.maven.model.Model#setVersion() . These examples are extracted from open source projects. 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 Project: butterfly   File: ModelTree.java    License: MIT License 6 votes vote down vote up
/**
     * This is a tree of Maven artifacts, which are represented by {@link Model} objects.
     * The idea here is, given a list of Maven pom.xml {@link File} objects, create a tree
     * based on dependency among them, but specifying explicitly which Maven artifact should
     * be at the root of the tree. That means, if any artifact in the list is not a child,
     * directly or indirectly, of the root artifact, then it will end up no being in the tree.
     * <br>
     * As a result of building this tree, it is possible to know, out of the initial pom.xml files list,
     * which ones actually inherit, directly or not, from the root artifact. The result is retrieved
     * by calling {@link #getPomFilesInTree()}
     *
     * @param rootGroupId the group id of the artifact that should be at the root of the tree
     * @param rootArtifactId the artifact id of the artifact that should be at the root of the tree
     * @param rootVersion the version of the artifact that should be at the root of the tree
     * @param pomFiles a list of pom.xml files used to make the tree
     */
    public ModelTree(String rootGroupId, String rootArtifactId, String rootVersion, List<File> pomFiles) {
        Model rootModel = new Model();
        rootModel.setGroupId(rootGroupId);
        rootModel.setArtifactId(rootArtifactId);
        rootModel.setVersion(rootVersion);

        List<Model> models = new ArrayList<>();
        models.add(rootModel);

        for (File pomFile : pomFiles) {
            models.add(createModel(pomFile));
        }

// TODO we can comment this out in the future if we need this feature
//        modelsNotInTree = add(models);
        add(models);
    }
 
Example 2
protected ITalendProcessJavaProject createServiceJavaProject() {
    IProgressMonitor monitor = new NullProgressMonitor();

    // temp model.
    Model model = new Model();
    model.setModelVersion("4.0.0"); //$NON-NLS-1$
    model.setGroupId(PomIdsHelper.getJobGroupId(serviceItem.getProperty()));
    model.setArtifactId(PomIdsHelper.getJobArtifactId(serviceItem.getProperty())); // $NON-NLS-1$
    model.setVersion(PomIdsHelper.getProjectVersion());
    model.setPackaging(TalendMavenConstants.PACKAGING_JAR);

    // always use temp model to avoid classpath problem
    final ProjectImportConfiguration importConfiguration = new ProjectImportConfiguration();
    IProject root = ResourcesPlugin.getWorkspace().getRoot()
            .getProject(ProjectManager.getInstance().getCurrentProject().getTechnicalLabel());
    try {
        return createSimpleProject(monitor, root, model, importConfiguration);
    } catch (CoreException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 3
/**
 * Changes the project version of the POM as well as directly in the XML document preserving the whole document
 * formatting.
 *
 * @param model the POM where to adapt the project version.
 * @param document the POM as an XML document in which the project version shall be adapted.
 * @param newVersion the new project version to set.
 */
public static void setProjectVersion(Model model, Document document, String newVersion) {
  Preconditions.checkArgument(hasChildNode(document, NODE_NAME_PROJECT),
      "The document doesn't seem to be a POM model, project element is missing.");

  // if model version is null, the parent version is inherited
  if (model.getVersion() != null) {
    // first step: update the version of the in-memory project
    model.setVersion(newVersion);

    // second step: update the project version in the DOM document that is then serialized for later building
    NodeList children = document.getDocumentElement().getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
      Node child = children.item(i);
      if (Objects.equal(child.getNodeName(), PomUtil.NODE_NAME_VERSION)) {
        child.setTextContent(newVersion);
      }
    }
  }
}
 
Example 4
Source Project: maven-git-versioning-extension   File: GAVTest.java    License: MIT License 6 votes vote down vote up
@Test
void of_model() {
    // Given
    Model model = new Model();
    model.setGroupId("group");
    model.setArtifactId("artifact");
    model.setVersion("version");

    // When
    GAV gav = GAV.of(model);

    // Then
    assertThat(gav).isNotNull();
    assertThat(gav.getGroupId()).isEqualTo("group");
    assertThat(gav.getArtifactId()).isEqualTo("artifact");
    assertThat(gav.getVersion()).isEqualTo("version");
}
 
Example 5
Source Project: maven-git-versioning-extension   File: GAVTest.java    License: MIT License 6 votes vote down vote up
@Test
void of_model_withParent_noInheritance() {
    // Given
    Model model = new Model();
    model.setGroupId("group");
    model.setArtifactId("artifact");
    model.setVersion("version");

    Parent parent = new Parent();
    parent.setGroupId("parentGroup");
    parent.setArtifactId("parentArtifact");
    parent.setVersion("parentVersion");
    model.setParent(parent);

    // When
    GAV gav = GAV.of(model);

    // Then
    assertThat(gav).isNotNull();
    assertThat(gav.getGroupId()).isEqualTo("group");
    assertThat(gav.getArtifactId()).isEqualTo("artifact");
    assertThat(gav.getVersion()).isEqualTo("version");
}
 
Example 6
private StorageAsset createPom(StorageAsset targetPath, String filename, FileMetadata fileMetadata, String groupId,
                       String artifactId, String version, String packaging)
        throws IOException {
    Model projectModel = new Model();
    projectModel.setModelVersion("4.0.0");
    projectModel.setGroupId(groupId);
    projectModel.setArtifactId(artifactId);
    projectModel.setVersion(version);
    projectModel.setPackaging(packaging);

    StorageAsset pomFile = targetPath.resolve(filename);
    MavenXpp3Writer writer = new MavenXpp3Writer();

    try (Writer w = new OutputStreamWriter(pomFile.getWriteStream(true))) {
        writer.write(w, projectModel);
    }

    return pomFile;
}
 
Example 7
@Test
public void projectDeploySkipTurnedOffWhenModeIsOff()
    throws Exception
{
    final Plugin plugin = new Plugin();
    plugin.setGroupId( MAVEN_PLUGIN_GROUPID );
    plugin.setArtifactId( MAVEN_DEPLOY_ARTIFACTID );
    plugin.setConfiguration( simpleSkipConfig( true ) );

    final Build build = new Build();
    build.addPlugin( plugin );

    final Model model = new Model();
    model.setModelVersion( "4.0.0" );
    model.setGroupId( "org.foo" );
    model.setArtifactId( "bar" );
    model.setVersion( "1" );

    model.setBuild( build );

    applyTest( off, model, model );
    assertSkip( model, null );
}
 
Example 8
@Test
public void projectUnchangedWhenModeIsNone()
    throws Exception
{
    final Plugin plugin = new Plugin();
    plugin.setGroupId( MAVEN_PLUGIN_GROUPID );
    plugin.setArtifactId( MAVEN_DEPLOY_ARTIFACTID );
    plugin.setConfiguration( simpleSkipConfig( true ) );

    final Build build = new Build();
    build.addPlugin( plugin );

    final Model model = new Model();
    model.setModelVersion( "4.0.0" );
    model.setGroupId( "org.foo" );
    model.setArtifactId( "bar" );
    model.setVersion( "1" );

    model.setBuild( build );

    applyTest( none, model, null );
}
 
Example 9
public MavenPomFileGenerator(MavenProjectIdentity identity) {
    mavenProject.setModelVersion(POM_VERSION);
    Model model = getModel();
    model.setGroupId(identity.getGroupId());
    model.setArtifactId(identity.getArtifactId());
    model.setVersion(identity.getVersion());
}
 
Example 10
public MavenPomFileGenerator(MavenProjectIdentity identity) {
    mavenProject.setModelVersion(POM_VERSION);
    Model model = getModel();
    model.setGroupId(identity.getGroupId());
    model.setArtifactId(identity.getArtifactId());
    model.setVersion(identity.getVersion());
}
 
Example 11
public MavenPomFileGenerator(MavenProjectIdentity identity) {
    mavenProject.setModelVersion(POM_VERSION);
    Model model = getModel();
    model.setGroupId(identity.getGroupId());
    model.setArtifactId(identity.getArtifactId());
    model.setVersion(identity.getVersion());
}
 
Example 12
Source Project: pom-manipulation-ext   File: PomIOTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testGAVReturnPOMs()
                throws Exception
{
    URL resource = PomIOTest.class.getResource( filename );
    assertNotNull( resource );
    File pom = new File( resource.getFile() );
    assertTrue( pom.exists() );

    File targetFile = folder.newFile( "target.xml" );
    FileUtils.copyFile( pom, targetFile );

    Model model = new Model();
    model.setGroupId( "org.commonjava.maven.ext.versioning.test" );
    model.setArtifactId( "dospom" );
    model.setVersion( "1.0" );
    model.setPackaging( "pom" );
    model.setModelVersion( "4.0.0" );

    Project p = new Project( targetFile, model );
    p.setExecutionRoot();
    HashSet<Project> changed = new HashSet<>();
    changed.add( p );

    ProjectVersionRef gav = pomIO.rewritePOMs( changed );

    assertEquals( "1.0", gav.getVersionString() );
    assertEquals( "org.commonjava.maven.ext.versioning.test", gav.getGroupId() );
    assertEquals( "dospom", gav.getArtifactId() );
}
 
Example 13
Source Project: pom-manipulation-ext   File: PomIOTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testRewritePOMs()
                throws Exception
{
    URL resource = PomIOTest.class.getResource( filename );
    assertNotNull( resource );
    File pom = new File( resource.getFile() );
    assertTrue( pom.exists() );

    File targetFile = folder.newFile( "target.xml" );
    FileUtils.writeStringToFile( targetFile, FileUtils.readFileToString( pom, StandardCharsets.UTF_8 )
                                                      .replaceAll( "dospom", "newdospom" ), StandardCharsets.UTF_8 );
    FileUtils.copyFile( pom, targetFile );

    List<Project> projects = pomIO.parseProject( targetFile );
    Model model = projects.get( 0 ).getModel();
    model.setGroupId( "org.commonjava.maven.ext.versioning.test" );
    model.setArtifactId( "dospom" );
    model.setVersion( "1.0" );
    model.setPackaging( "pom" );
    model.setModelVersion( "4.0.0" );

    Project p = new Project( targetFile, model );
    HashSet<Project> changed = new HashSet<>();
    changed.add( p );
    pomIO.rewritePOMs( changed );

    assertTrue( FileUtils.contentEqualsIgnoreEOL( pom, targetFile, StandardCharsets.UTF_8.toString() ) );
    assertTrue( FileUtils.contentEquals( targetFile, pom ) );
}
 
Example 14
@Test
public void incrementExistingSerialSuffix_TwoProjects_UsingRepositoryMetadata_DifferentAvailableIncrements()
    throws Exception
{
    final String v = "1.2.0.GA";
    final String os = "-foo-1";
    final String ns = "foo-10";

    final Model m1 = new Model();
    m1.setGroupId( GROUP_ID );
    m1.setArtifactId( ARTIFACT_ID );
    m1.setVersion( v + os );
    final Project p1 = new Project( m1 );

    final String a2 = ARTIFACT_ID + "-dep";
    final Model m2 = new Model();
    m2.setGroupId( GROUP_ID );
    m2.setArtifactId( a2 );
    m2.setVersion( v + os );
    final Project p2 = new Project( m2 );

    final Properties props = new Properties();

    props.setProperty( VersioningState.INCREMENT_SERIAL_SUFFIX_SYSPROP, "foo-0" );
    final Map<ProjectRef, String[]> versionMap = new HashMap<>();

    versionMap.put( new SimpleProjectRef( p1.getGroupId(), p1.getArtifactId() ), new String[] { "1.2.0.GA-foo-3",
        "1.2.0.GA-foo-2", "1.2.0.GA-foo-9" } );
    versionMap.put( new SimpleProjectRef( p2.getGroupId(), p2.getArtifactId() ), new String[] { "1.2.0.GA-foo-3",
        "1.2.0.GA-foo-2" } );

    setupSession( props, versionMap );

    final Map<ProjectVersionRef, String>
                    result = modder.calculateVersioningChanges( Arrays.asList( p1, p2 ), session );

    assertThat( result.get( new SimpleProjectVersionRef( GROUP_ID, ARTIFACT_ID, v + os ) ), equalTo( v + "-" + ns ) );
    assertThat( result.get( new SimpleProjectVersionRef( GROUP_ID, a2, v + os ) ), equalTo( v + "-" + ns ) );
}
 
Example 15
@Test(expected = ValidationErrorsException.class)
public void testValidatePom_missingGroupId() {
  Model model = new Model();
  model.setArtifactId("testArtifact");
  model.setVersion("1.0");
  underTest.validatePom(model);
}
 
Example 16
Source Project: pom-manipulation-ext   File: PomIOTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testWriteModel()
                throws Exception
{
    // Thanks to http://www.buildmystring.com
    String sb = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                    + "<project xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\" xmlns=\"http://maven.apache.org/POM/4.0.0\"\n"
                    + "    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n"
                    + "  <modelVersion>4.0.0</modelVersion>\n"
                    + "  <groupId>org.commonjava.maven.ext.versioning.test</groupId>\n"
                    + "  <artifactId>dospom</artifactId>\n" + "  <version>1.0</version>\n"
                    + "  <packaging>pom</packaging>\n" + "</project>\n";

    URL resource = PomIOTest.class.getResource( filename );
    assertNotNull( resource );
    File pom = new File( resource.getFile() );
    assertTrue( pom.exists() );

    File targetFile = folder.newFile( "target.xml" );

    Model model = new Model();
    model.setGroupId( "org.commonjava.maven.ext.versioning.test" );
    model.setArtifactId( "dospom" );
    model.setVersion( "1.0" );
    model.setPackaging( "pom" );
    model.setModelVersion( "4.0.0" );

    pomIO.writeModel( model, targetFile );
    assertTrue( targetFile.exists() );
    assertEquals( sb, FileUtils.readFileToString( targetFile, StandardCharsets.UTF_8 ) );
}
 
Example 17
private Model createModel( Project project, String s )
{
   final Model newModel = new Model();
    newModel.setModelVersion( project.getModel().getModelVersion() );

    newModel.setGroupId( project.getGroupId() + '.' + project.getArtifactId() );
    newModel.setArtifactId( s );
    newModel.setVersion( project.getVersion() );
    newModel.setPackaging( "pom" );

    return newModel;
}
 
Example 18
@Test(expected = ValidationErrorsException.class)
public void testValidatePom_versionWithProperty() {
  Model model = new Model();
  model.setArtifactId("testArtifact");
  model.setGroupId("testGroup");
  model.setVersion("${aProperty}");
  underTest.validatePom(model);
}
 
Example 19
Source Project: developer-studio   File: MavenUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static MavenProject createMavenProject(String groupId, String artifactId, String version, String packagingType) {
	Model model = new Model();
	model.setGroupId(groupId);
	model.setArtifactId(artifactId);
	model.setVersion(version);
	model.setModelVersion("4.0.0");
	model.setName(artifactId);
	model.setDescription(artifactId);
	if (packagingType!=null){
		model.setPackaging(packagingType);
	}
	return new MavenProject(model);
}
 
Example 20
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 );
    }


}