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

The following examples show how to use org.apache.maven.model.Model#getVersion() . 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: QuarkusJsonPlatformDescriptorResolver.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private static String getVersion(Model model) {
    if (model == null) {
        return null;
    }
    String version = model.getVersion();
    if (version != null) {
        return version;
    }
    final Parent parent = model.getParent();
    if (parent != null) {
        version = parent.getVersion();
        if (version != null) {
            return version;
        }
    }
    throw new IllegalStateException("Failed to determine the version for the POM of " + model.getArtifactId());
}
 
Example 2
Source File: GAV.java    From maven-git-versioning-extension with MIT License 6 votes vote down vote up
public static GAV of(Model model) {

        String groupId = model.getGroupId();
        String artifactId = model.getArtifactId();
        String version = model.getVersion();

        Parent parent = model.getParent();
        if (parent != null) {
            if (groupId == null) {
                groupId = parent.getGroupId();
            }
            if (version == null) {
                version = parent.getVersion();
            }
        }

        return new GAV(groupId, artifactId, version);
    }
 
Example 3
Source File: PomUtil.java    From unleash-maven-plugin with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 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 File: CarnotzetModuleCoordinates.java    From carnotzet with Apache License 2.0 6 votes vote down vote up
public static CarnotzetModuleCoordinates fromPom(@NonNull Path pom) {
	Model result;
	try {
		BufferedReader in = new BufferedReader(Files.newBufferedReader(pom, StandardCharsets.UTF_8));
		MavenXpp3Reader reader = new MavenXpp3Reader();
		result = reader.read(in);
	}
	catch (XmlPullParserException | IOException e) {
		throw new CarnotzetDefinitionException(e);
	}
	String groupId = result.getGroupId();
	String version = result.getVersion();
	if (groupId == null) {
		groupId = result.getParent().getGroupId();
	}
	if (version == null) {
		version = result.getParent().getVersion();
	}
	return new CarnotzetModuleCoordinates(groupId, result.getArtifactId(), version, null);
}
 
Example 5
Source File: VersionChecker.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
public VersionChecker(ResourceFetcher resourceFetcher) throws VersionCheckException {
	try {
		byte[] data = resourceFetcher.getData("pom.xml");
		if (data == null) {
			throw new VersionCheckException("No pom.xml found");
		}

		String version = null;
		MavenXpp3Reader mavenreader = new MavenXpp3Reader();
		if (data != null) {
			Model model = mavenreader.read(new ByteArrayInputStream(data));
			version = model.getVersion();
		}					

		if (version == null) {
			// Only on development environments we have to get the version from the parent pom file
			byte[] parentData = resourceFetcher.getData("../pom.xml");
			if (parentData != null) {
				Model parentModel = mavenreader.read(new ByteArrayInputStream(parentData));
				version = parentModel.getVersion();
			} else {
				LOGGER.error("No parent pom.xml found");
			}
		}
		
		DefaultArtifactVersion defaultArtifactVersion = new DefaultArtifactVersion(version);
		localVersion = new SVersion();
		localVersion.setMajor(defaultArtifactVersion.getMajorVersion());
		localVersion.setMinor(defaultArtifactVersion.getMinorVersion());
		localVersion.setRevision(defaultArtifactVersion.getIncrementalVersion());
		localVersion.setFullString(defaultArtifactVersion.toString());
	} catch (Exception e) {
		throw new VersionCheckException(e);
	}
}
 
Example 6
Source File: PomHelper.java    From versions-maven-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Extracts the version from a raw model, interpolating from the parent if necessary.
 *
 * @param model The model.
 * @return The version.
 */
public static String getVersion( Model model )
{
    String targetVersion = model.getVersion();
    if ( targetVersion == null && model.getParent() != null )
    {
        targetVersion = model.getParent().getVersion();
    }
    return targetVersion;
}
 
Example 7
Source File: MavenRepositoryDeployer.java    From maven-repository-tools with Eclipse Public License 1.0 5 votes vote down vote up
public static Gav getCoordinates ( File pomFile ) throws Exception
{
    BufferedReader in = new BufferedReader( new FileReader( pomFile ) );
    MavenXpp3Reader reader = new MavenXpp3Reader();
    Model model = reader.read( in );
    // get coordinates and take care of inheritance and default
    String g = model.getGroupId();
    if ( StringUtils.isEmpty( g ) ) 
    {
        g = model.getParent().getGroupId();
    }
    String a = model.getArtifactId();
    if ( StringUtils.isEmpty( a ) ) 
    {
        a = model.getParent().getArtifactId();
    }
    String v = model.getVersion();
    if ( StringUtils.isEmpty( v ) ) 
    {
        v = model.getParent().getVersion();
    }
    String p = model.getPackaging();
    if ( StringUtils.isEmpty( p ) ) 
    {
        p = MavenConstants.JAR;
    }
    Gav gav = new Gav( g, a, v, p );
    return gav;
}
 
Example 8
Source File: Project.java    From pom-manipulation-ext with Apache License 2.0 5 votes vote down vote up
public Project( final File pom, final Model model ) throws ManipulationException
{
    this.pom = pom;
    this.model = model;

    // Validate the model.
    if ( model == null )
    {
        throw new ManipulationException( "Invalid null model." );
    }
    else if ( model.getVersion() == null && model.getParent() == null )
    {
        throw new ManipulationException( "Invalid model ({}) - cannot find version!" );
    }
}
 
Example 9
Source File: MavenRunner.java    From app-runner with MIT License 5 votes vote down vote up
public void start(LineConsumer buildLogHandler, LineConsumer consoleLogHandler, Map<String, String> envVarsForApp, Waiter startupWaiter) throws ProjectCannotStartException {
    File pomFile = new File(projectRoot, "pom.xml");

    if (goals.isEmpty()) {
        log.info("No goals. Skipping maven build");

    } else {
        InvocationRequest request = new DefaultInvocationRequest()
            .setBatchMode(true)
            .setPomFile(pomFile)
            .setOutputHandler(buildLogHandler::consumeLine)
            .setErrorHandler(buildLogHandler::consumeLine)
            .setGoals(goals)
            .setBaseDirectory(projectRoot);


        log.info("Building maven project at " + fullPath(projectRoot));
        runRequest(request, javaHomeProvider);
        log.info("Build successful. Going to start app.");
    }

    Model model = loadPomModel(pomFile);
    String jarName = model.getArtifactId() + "-" + model.getVersion() + ".jar";

    File jar = new File(new File(projectRoot, "target"), jarName);
    if (!jar.isFile()) {
        throw new ProjectCannotStartException("Could not find the jar file at " + fullPath(jar));
    }

    CommandLine command = javaHomeProvider.commandLine(envVarsForApp)
        .addArgument("-Djava.io.tmpdir=" + envVarsForApp.get("TEMP"))
        .addArgument("-jar")
        .addArgument("target" + File.separator + jarName);

    watchDog = ProcessStarter.startDaemon(buildLogHandler, consoleLogHandler, envVarsForApp, command, projectRoot, startupWaiter);
}
 
Example 10
Source File: TestPlugin.java    From minikube-build-tools-for-java with Apache License 2.0 5 votes vote down vote up
@Override
protected void before() throws IOException, XmlPullParserException, VerificationException {
  // Installs the plugin for use in tests.
  Verifier verifier = new Verifier(".", true);
  verifier.setAutoclean(false);
  verifier.addCliOption("-DskipTests");
  verifier.executeGoal("install");

  // Reads the project version.
  MavenXpp3Reader reader = new MavenXpp3Reader();
  Model model = reader.read(new FileReader("pom.xml"));
  pluginVersion = model.getVersion();
}
 
Example 11
Source File: ProjectVersionManipulatorTest.java    From pom-manipulation-ext with Apache License 2.0 5 votes vote down vote up
@Test
public void updateEffectiveAndOriginalModelMainVersions()
    throws Exception
{
    final Model orig = new Model();
    orig.setGroupId( "org.foo" );
    orig.setArtifactId( "bar" );
    orig.setVersion( "1.0" );

    final Model eff = orig.clone();

    final String suff = AddSuffixJettyHandler.DEFAULT_SUFFIX;
    final String mv = orig.getVersion() + "." + suff;

    final Map<ProjectVersionRef, String> versionsByGAV = new HashMap<>();
    versionsByGAV.put( new SimpleProjectVersionRef( orig.getGroupId(), orig.getArtifactId(), orig.getVersion() ), mv );

    final MavenProject project = new MavenProject( eff );
    project.setOriginalModel( orig );

    final Set<MavenProject> changes =
        newVersioningModifier().applyVersioningChanges( Collections.singletonList( project ), versionsByGAV );

    assertThat( changes.size(), equalTo( 1 ) );
    assertThat( orig.getVersion(), equalTo( mv ) );
    assertThat( eff.getVersion(), equalTo( mv ) );
}
 
Example 12
Source File: ModelUtils.java    From quarkus with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the raw version of the model. If the model does not include
 * the version directly, it will return the version of the parent.
 * The version is raw in a sense if it's a property expression, the
 * expression will not be resolved.
 *
 * @param model POM
 * @return raw model
 */
public static String getRawVersion(Model model) {
    String version = model.getVersion();
    if (version != null) {
        return version;
    }
    final Parent parent = model.getParent();
    if (parent != null) {
        version = parent.getVersion();
        if (version != null) {
            return version;
        }
    }
    throw new IllegalStateException("Failed to determine version for project model");
}
 
Example 13
Source File: MavenUtil.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
private static String getArtifactFile(final File dir) {
  try {
    final Model model = getModel(new File(dir, "pom.xml"));
    final String version = model.getVersion() != null ? model.getVersion() : model.getParent().getVersion();
    return model.getArtifactId() + "-" + version + ".jar";
  }
  catch (final IOException | XmlPullParserException e) {
    throw new IllegalStateException(e);
  }
}
 
Example 14
Source File: MavenUtil.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
public static String getArtifactVersion(final File dir) {
  try {
    final Model model = getModel(new File(dir, "pom.xml"));
    return model.getVersion() != null ? model.getVersion() : model.getParent().getVersion();
  }
  catch (final IOException | XmlPullParserException e) {
    throw new IllegalStateException(e);
  }
}
 
Example 15
Source File: DeployUtil.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
private static String getPomVersion() {
    try {
        MavenXpp3Reader pomReader = new MavenXpp3Reader();
        Model model = pomReader
                .read(new InputStreamReader(new FileInputStream("../pom.xml"), StandardCharsets.UTF_8));
        return model.getVersion();
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
 
Example 16
Source File: CqUtils.java    From camel-quarkus with Apache License 2.0 4 votes vote down vote up
static String getVersion(Model basePom) {
    return basePom.getVersion() != null ? basePom.getVersion()
            : basePom.getParent() != null && basePom.getParent().getVersion() != null
                    ? basePom.getParent().getVersion()
                    : null;
}
 
Example 17
Source File: CreateExtensionMojo.java    From quarkus with Apache License 2.0 4 votes vote down vote up
static String getVersion(Model basePom) {
    return basePom.getVersion() != null ? basePom.getVersion()
            : basePom.getParent() != null && basePom.getParent().getVersion() != null
                    ? basePom.getParent().getVersion()
                    : null;
}
 
Example 18
Source File: MojoUtils.java    From quarkus with Apache License 2.0 4 votes vote down vote up
public static String[] readGavFromPom(final InputStream resourceAsStream) throws IOException {
    Model model = readPom(resourceAsStream);
    return new String[] { model.getGroupId(), model.getArtifactId(), model.getVersion() };
}
 
Example 19
Source File: LeinRunner.java    From app-runner with MIT License 3 votes vote down vote up
public void start(LineConsumer buildLogHandler, LineConsumer consoleLogHandler, Map<String, String> envVarsForApp, Waiter startupWaiter) throws ProjectCannotStartException {

        runLein(buildLogHandler, envVarsForApp, "do", "test,", "uberjar,", "pom");

        Model model = loadPomModel(new File(projectRoot, "pom.xml"));
        String jarName = model.getArtifactId() + "-" + model.getVersion() + "-standalone.jar";

        CommandLine command = javaCmd.commandLine(envVarsForApp);
        command.addArgument("-jar").addArgument("target" + File.separator + jarName);

        watchDog = ProcessStarter.startDaemon(buildLogHandler, consoleLogHandler, envVarsForApp, command, projectRoot, startupWaiter);
    }
 
Example 20
Source File: PomHelper.java    From versions-maven-plugin with Apache License 2.0 2 votes vote down vote up
/**
 * Checks to see if the model contains an explicitly specified version.
 *
 * @param model The model.
 * @return {@code true} if the model explicitly specifies the project version, i.e. /project/version
 */
public static boolean isExplicitVersion( Model model )
{
    return model.getVersion() != null;
}