Java – Working with Maven POM through java code.

import java.io.File;
import java.io.FileReader;

import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.project.MavenProject;

/**
 * Add these files in classpath
 * maven-artifact-2.0.6.jar,maven-core-2.0.6.jar,
 * maven-model-2.0.6.jar,maven-project-2.0.6.jar,
 * plexus-utils-2.0.7.jar
 * 
 * @author polimeb
 * 
 */
public class ParseMavenPOM {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		String fileName = "c:/project/abcd/pom.xml";

		try {
			Model model = null;
			FileReader reader = null;
			MavenXpp3Reader mavenreader = new MavenXpp3Reader();

			File pomfile = new File(fileName);
			reader = new FileReader(pomfile);
			model = mavenreader.read(reader);

			MavenProject project = new MavenProject(model);
			System.out.println(project.getVersion());

		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}

}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s