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

The following examples show how to use org.apache.maven.model.Parent#setVersion() . 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: MvnProjectBuilder.java    From quarkus with Apache License 2.0 6 votes vote down vote up
public MvnProjectBuilder addModule(String path, String artifactId, boolean initParent) {
    model.addModule(path);
    final MvnProjectBuilder module = new MvnProjectBuilder(artifactId, this);
    if (initParent) {
        final Parent parentModel = new Parent();
        module.model.setParent(parentModel);
        parentModel.setGroupId(model.getGroupId());
        parentModel.setArtifactId(model.getArtifactId());
        parentModel.setVersion(model.getVersion());
        final Path rootDir = Paths.get("").toAbsolutePath();
        final Path moduleDir = rootDir.resolve(path).normalize();
        if (!moduleDir.getParent().equals(rootDir)) {
            parentModel.setRelativePath(moduleDir.relativize(rootDir).toString());
        }
    }
    modules.add(module);
    return module;
}
 
Example 2
Source File: PomUtil.java    From unleash-maven-plugin with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Changes the project's parent 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's parent version.
 * @param document the POM as an XML document in which the project's parent version shall be adapted.
 * @param newParentVersion the new version to set for the project parent.
 */
public static void setParentVersion(Model model, Document document, String newParentVersion) {
  Preconditions.checkArgument(hasChildNode(document, NODE_NAME_PROJECT),
      "The document doesn't seem to be a POM model, project element is missing.");

  // first step: update parent version of the in-memory model
  Parent parent = model.getParent();
  if (parent != null) {
    parent.setVersion(newParentVersion);
  }

  // second step: update the parent version in the DOM document that will be serialized for later building
  Node parentNode = document.getDocumentElement().getElementsByTagName(PomUtil.NODE_NAME_PARENT).item(0);
  if (parentNode != null) {
    NodeList children = parentNode.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(newParentVersion);
      }
    }
  }
}
 
Example 3
Source File: GAVTest.java    From maven-git-versioning-extension with 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 4
Source File: GAVTest.java    From maven-git-versioning-extension with MIT License 6 votes vote down vote up
@Test
void of_model_withParent_withInheritance() {
    // Given
    Model model = new Model();
    model.setArtifactId("artifact");

    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("parentGroup");
    assertThat(gav.getArtifactId()).isEqualTo("artifact");
    assertThat(gav.getVersion()).isEqualTo("parentVersion");
}
 
Example 5
Source File: MSF4JProjectImporter.java    From msf4j with Apache License 2.0 6 votes vote down vote up
private void saveMavenParentInfo(MavenProjectInfo projectInfo) throws IOException, XmlPullParserException {
	File mavenProjectPomLocation = projectInfo.getPomFile();// project.getFile(POM_FILE).getLocation().toFile();
	MavenProject mavenProject = null;
	mavenProject = MavenUtils.getMavenProject(mavenProjectPomLocation);
	Parent msf4jParent = new Parent();
	msf4jParent.setGroupId(MSF4J_SERVICE_PARENT_GROUP_ID);
	msf4jParent.setArtifactId(MSF4J_SERVICE_PARENT_ARTIFACT_ID);
	msf4jParent.setVersion(MSF4JArtifactConstants.getMSF4JServiceParentVersion());
	mavenProject.getModel().setParent(msf4jParent);

	Properties generatedProperties = mavenProject.getModel().getProperties();
	generatedProperties.clear();

	mavenProject.getModel().addProperty(MSF4J_MAIN_CLASS_PROPERTY, DEFAULT_MAIN_CLASS_PROPERTY_VALUE);
	MavenUtils.saveMavenProject(mavenProject, mavenProjectPomLocation);
}
 
Example 6
Source File: NewGraniteProjectWizard.java    From aem-eclipse-developer-tools with Apache License 2.0 6 votes vote down vote up
private void fixParentProject(IProject p, IProject parentProject)
		throws CoreException {
	IFile existingPom = p.getFile("pom.xml");
	Model model = MavenPlugin.getMavenModelManager().readMavenModel(existingPom);
	Model parent = MavenPlugin.getMavenModelManager().readMavenModel(parentProject.getFile("pom.xml"));
	//Parent oldParent = model.getParent();
	Parent newParent = new Parent();
	newParent.setGroupId(parent.getGroupId());
	newParent.setArtifactId(parent.getArtifactId());
	newParent.setRelativePath(calculateRelativePath(p, parentProject));
	newParent.setVersion(parent.getVersion());
	model.setParent(newParent);
	// outright deletion doesn't work on windows as the process has a ref to the file itself
	// so creating a temp '_newpom_.xml'
	final IFile newPom = p.getFile("_newpom_.xml");
	MavenPlugin.getMavenModelManager().createMavenModel(newPom, model);
	// then copying that content over to the pom.xml
	existingPom.setContents(newPom.getContents(), true,  true, new NullProgressMonitor());
	// and deleting the temp pom
	newPom.delete(true,  false, new NullProgressMonitor());
	
}
 
Example 7
Source File: ParentInjectionState.java    From pom-manipulation-ext with Apache License 2.0 6 votes vote down vote up
public void initialise( Properties userProps )
{
    final String gav = userProps.getProperty( PARENT_INJECTION_PROPERTY );

    if ( gav != null )
    {
        ProjectVersionRef ref = SimpleProjectVersionRef.parse( gav );
        parent = new Parent();
        parent.setGroupId( ref.getGroupId() );
        parent.setArtifactId( ref.getArtifactId() );
        parent.setVersion( ref.getVersionString() );
        parent.setRelativePath( "" );
    }
    else
    {
        parent = null;
    }
}
 
Example 8
Source File: PomChangeParent.java    From butterfly with MIT License 5 votes vote down vote up
@Override
protected TOExecutionResult pomExecution(String relativePomFile, Model model) {
    String details;
    Parent parent = model.getParent();

    if (parent == null) {
        String message = String.format("Pom file %s does not have a parent", getRelativePath());

        switch (ifNotPresent) {
            case Warn:
                return TOExecutionResult.warning(this, new TransformationOperationException(message));
            case NoOp:
                return TOExecutionResult.noOp(this, message);
            case Fail:
                // Fail is the default
            default:
                return TOExecutionResult.error(this, new TransformationOperationException(message));
        }
    }

    if(groupId != null && artifactId != null && version != null) {
        parent.setGroupId(groupId);
        parent.setArtifactId(artifactId);
        parent.setVersion(version);
        String newParent = parent.toString();
        details = String.format("Parent for POM file (%s) has been set to %s", relativePomFile, newParent);
    } else if (groupId == null && artifactId == null && version != null) {
        String oldVersion = parent.getVersion();
        parent.setVersion(version);
        details = String.format("Parent's version for POM file (%s) has been changed from %s to %s", relativePomFile, oldVersion, version);
    } else {
        // FIXME this should be in a pre-validation
        return TOExecutionResult.error(this, new TransformationOperationException("Invalid POM parent transformation operation"));
    }

    return TOExecutionResult.success(this, details);
}
 
Example 9
Source File: MSF4JArtifactProjectNature.java    From msf4j with Apache License 2.0 5 votes vote down vote up
/**
 * Update created pom.xml file with necessary dependencies and plug-ins so
 * that it works with WSO2 MSF4J server
 * 
 * @throws IOException
 * @throws XmlPullParserException
 *
 */
private void updatePom(IProject project) throws IOException, XmlPullParserException {
	File mavenProjectPomLocation = project.getFile(POM_FILE).getLocation().toFile();
	MavenProject mavenProject = MavenUtils.getMavenProject(mavenProjectPomLocation);
	Parent msf4jParent = new Parent();
	msf4jParent.setGroupId(MSF4J_SERVICE_PARENT_GROUP_ID);
	msf4jParent.setArtifactId(MSF4J_SERVICE_PARENT_ARTIFACT_ID);
	msf4jParent.setVersion(MSF4JArtifactConstants.getMSF4JServiceParentVersion());
	mavenProject.getModel().setParent(msf4jParent);

	Properties generatedProperties = mavenProject.getModel().getProperties();
	generatedProperties.clear();

}
 
Example 10
Source File: MavenDetailsPage.java    From developer-studio with Apache License 2.0 5 votes vote down vote up
private Parent getParentFromPreferernceStore() {
	Parent parent = new Parent();
	parent.setGroupId(preferencesService.getString("org.wso2.developerstudio.eclipse.platform.ui",
	                                               GLOBAL_PARENT_MAVEN_GROUP_ID, null, null));
	parent.setArtifactId(preferencesService.getString("org.wso2.developerstudio.eclipse.platform.ui",
	                                                  GLOBAL_PARENT_MAVEN_ARTIFACTID, null, null));
	parent.setVersion(preferencesService.getString("org.wso2.developerstudio.eclipse.platform.ui",
	                                               GLOBAL_PARENT_MAVEN_VERSION, null, null));
	parent.setRelativePath(null);
	return parent;
}
 
Example 11
Source File: MavenDetailsPage.java    From developer-studio with Apache License 2.0 5 votes vote down vote up
private void updateParent() {
	Parent parent = new Parent();
	parent.setArtifactId(getParentArtifactID());
	parent.setGroupId(getParentGroupID());
	parent.setVersion(getParentVersion());
	parent.setRelativePath(getParentRelativePath());
	mavenProjectInfo.setParentProject(parent);
	dataModel.setMavenInfo(mavenProjectInfo);
}
 
Example 12
Source File: LocalWorkspaceDiscoveryTest.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@BeforeAll
public static void setup() throws Exception {
    workDir = IoUtils.createRandomTmpDir();

    systemPropertiesBackup = (Properties) System.getProperties().clone();

    final Parent parent = new Parent();
    parent.setGroupId(MvnProjectBuilder.DEFAULT_GROUP_ID);
    parent.setArtifactId("parent");
    parent.setVersion(MvnProjectBuilder.DEFAULT_VERSION);
    parent.setRelativePath(null);

    final Parent parentWithEmptyRelativePath = new Parent();
    parent.setGroupId(MvnProjectBuilder.DEFAULT_GROUP_ID);
    parent.setArtifactId("parent-empty-path");
    parent.setVersion(MvnProjectBuilder.DEFAULT_VERSION);
    parent.setRelativePath("");

    MvnProjectBuilder.forArtifact("root")
            .setParent(parent)

            .addModule("module1", "root-no-parent-module", false)
            .addDependency(newDependency("root-module-not-direct-child"))
            .getParent()

            .addModule("module2", "root-module-with-parent", true)
            .addDependency(newDependency("root-no-parent-module"))
            .addDependency(newDependency("external-dep"))
            .addDependency(newDependency(LocalProject.PROJECT_GROUPID, "root-module-not-direct-child",
                    MvnProjectBuilder.DEFAULT_VERSION))
            .getParent()

            .addModule("other/module3", "root-module-not-direct-child", true)
            .getParent()

            .addModule("module4", "empty-parent-relative-path-module").setParent(parentWithEmptyRelativePath)
            .getParent()

            .build(workDir.resolve("root"));

    final Parent rootParent = new Parent();
    rootParent.setGroupId(MvnProjectBuilder.DEFAULT_GROUP_ID);
    rootParent.setArtifactId("root");
    rootParent.setVersion(MvnProjectBuilder.DEFAULT_VERSION);
    rootParent.setRelativePath(null);

    MvnProjectBuilder.forArtifact("non-module-child")
            .setParent(rootParent)
            .addModule("module1", "another-child", true)
            .getParent()
            .build(workDir.resolve("root").resolve("non-module-child"));

    // independent project in the tree
    MvnProjectBuilder.forArtifact("independent")
            .addDependency(newDependency("root-module-with-parent"))
            .build(workDir.resolve("root").resolve("independent"));
}
 
Example 13
Source File: PomAddParent.java    From butterfly with MIT License 4 votes vote down vote up
@Override
protected TOExecutionResult pomExecution(File transformedAppFolder, TransformationContext transformationContext) throws XmlPullParserException, XMLStreamException, IOException {

    File pomFile = getAbsoluteFile(transformedAppFolder, transformationContext);
    Parent existingParent = getModel(pomFile).getParent();
    String details;

    String relativePomFile = getRelativePath(transformedAppFolder, pomFile);

    if (existingParent != null) {
        String message = String.format("Pom file %s already has a parent", relativePomFile);

        switch (ifPresent) {
            case WarnNotAdd:
                return TOExecutionResult.warning(this, message);
            case NoOp:
                return TOExecutionResult.noOp(this, message);
            case Fail:
                return TOExecutionResult.error(this, new TransformationOperationException(message));
            default:
                break;
        }
    }

    // FIXME this should be in a pre-validation
    if(groupId == null && artifactId == null && version == null) {
        throw new IllegalStateException("Invalid POM parent transformation operation");
    }

    Parent newParent = new Parent();
    newParent.setGroupId(groupId);
    newParent.setArtifactId(artifactId);
    newParent.setVersion(version);

    XMLEventReader reader = getReader(transformedAppFolder, transformationContext);
    XMLEventWriter writer = getWriter(transformedAppFolder, transformationContext);
    XMLEvent indentation = getIndentation(transformedAppFolder, transformationContext);

    TOExecutionResult result = null;

    if (existingParent != null) {
        copyUntil(reader, writer, new StartElementEventCondition("parent"), true);
        skipUntil(reader, new EndElementEventCondition("parent"));
        writer.add(LINE_FEED);

        writeNewParent(writer, indentation);

        writer.add(indentation);
        writer.add(eventFactory.createEndElement("", "","parent"));

        details = String.format("Parent for POM file %s has been overwritten to %s", relativePomFile, newParent);
        if (ifPresent.equals(IfPresent.Overwrite)) {
            result = TOExecutionResult.success(this, details);
        } else if (ifPresent.equals(IfPresent.WarnButAdd)) {
            result = TOExecutionResult.warning(this, details);
        }
    } else {
        copyUntil(reader, writer, new StartElementEventCondition("project"), true);

        writer.add(LINE_FEED);
        writer.add(LINE_FEED);
        writer.add(indentation);
        writer.add(eventFactory.createStartElement("", "", "parent"));
        writer.add(LINE_FEED);

        writeNewParent(writer, indentation);

        writer.add(indentation);
        writer.add(eventFactory.createEndElement("", "","parent"));
        writer.add(LINE_FEED);

        details = String.format("Parent for POM file %s has been set to %s", relativePomFile, newParent);
        result = TOExecutionResult.success(this, details);
    }

    writer.add(reader);

    return result;
}