Java Code Examples for org.apache.maven.model.io.xpp3.MavenXpp3Reader#read()

The following examples show how to use org.apache.maven.model.io.xpp3.MavenXpp3Reader#read() . 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: NBMNativeMWITest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Test
public void testPathNoParent() throws IOException, XmlPullParserException {
    clearWorkDir();
    wd = FileUtil.toFileObject(getWorkDir());
    FileObject createData = wd.createFolder("test1");
    ProjectInfo pi = new ProjectInfo("my.groupid", "artefact1", "1.0", "my.packagename");
    NBMNativeMWI.instantiate(pi, FileUtil.toFile(createData), "RELEASE110", true, null);

    FileObject builtpom = createData.getFileObject("pom", "xml");
    MavenXpp3Reader reader = new MavenXpp3Reader();
    Model model = reader.read(new FileReader(FileUtil.toFile(builtpom)));

    assertEquals("nbm-maven-plugin", model.getBuild().getPlugins().get(0).getArtifactId());
    assertEquals(MavenNbModuleImpl.getLatestNbmPluginVersion(), model.getBuild().getPlugins().get(0).getVersion());
    assertEquals("maven-compiler-plugin", model.getBuild().getPlugins().get(1).getArtifactId());
    assertEquals("3.8.1", model.getBuild().getPlugins().get(1).getVersion());
    assertEquals(0, model.getRepositories().size());
}
 
Example 2
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 3
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 4
Source File: ProjectStub.java    From docker-maven-plugin with Apache License 2.0 5 votes vote down vote up
public ProjectStub(File pom) {
  final MavenXpp3Reader pomReader = new MavenXpp3Reader();
  Model model;
  try {
    model = pomReader.read(ReaderFactory.newXmlReader(pom));
    setModel(model);
  } catch (Exception e) {
    throw new RuntimeException(e);
  }

  setGroupId(model.getGroupId());
  setArtifactId(model.getArtifactId());
  setVersion(model.getVersion());
  setName(model.getName());
  setUrl(model.getUrl());
  setPackaging(model.getPackaging());
  setBuild(model.getBuild());

  final List<String> compileSourceRoots = new ArrayList<>();
  compileSourceRoots.add(getBasedir() + "/src/main/java");
  setCompileSourceRoots(compileSourceRoots);

  final List<String> testCompileSourceRoots = new ArrayList<>();
  testCompileSourceRoots.add(getBasedir() + "/src/test/java");
  setTestCompileSourceRoots(testCompileSourceRoots);

  // normalize some expressions
  getBuild().setDirectory("${project.basedir}/target");
  getBuild().setTestOutputDirectory(new File(getBasedir(), "target/classes").getAbsolutePath());
}
 
Example 5
Source File: AbstractStaxPomOperation.java    From butterfly with MIT License 5 votes vote down vote up
protected Model getModel(File pomFile) throws IOException, XmlPullParserException {
    MavenXpp3Reader reader = new MavenXpp3Reader();
    FileInputStream fileInputStream = new FileInputStream(pomFile);
    Model model = reader.read(fileInputStream);
    fileInputStream.close();

    return model;
}
 
Example 6
Source File: BuildManager.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
private void rewritePOM(File pomFile) throws BuildException {
	try (Reader reader = new FileReader(pomFile)) {
		MavenXpp3Reader pomReader = new MavenXpp3Reader();
		
		Model model = pomReader.read(reader);
		reader.close();
		
		model.addRepository(createRepo("maven_central", "http://repo.maven.apache.org/maven2/", "default"));
		
		for (String id: eclipseRepos.keySet()) {
			model.addRepository(createRepo(id, eclipseRepos.get(id), "p2"));
		}
		
		Build modelBuild = model.getBuild();
		if (modelBuild == null) {
			model.setBuild(new Build());
		}
		
		model.getBuild().addPlugin(createPlugin("org.eclipse.tycho", "tycho-maven-plugin", "0.21.0", true));
		model.getBuild().addPlugin(createPlugin("org.eclipse.tycho", "target-platform-configuration", "0.21.0", false));
		model.getBuild().addPlugin(createPlugin("org.apache.maven.plugins", "maven-dependency-plugin", "2.8", false));
		
		MavenXpp3Writer pomWriter = new MavenXpp3Writer();
		pomWriter.write(new FileWriter(pomFile), model);
	} 
	catch (Exception e) {
		throw new BuildException("POM rewriting (to add plugin dependencies, cause) failed unexpectedly", e);
	}
}
 
Example 7
Source File: Application.java    From Mastering-Spring-Cloud with MIT License 5 votes vote down vote up
@Bean
public Docket api() throws IOException, XmlPullParserException {
	MavenXpp3Reader reader = new MavenXpp3Reader();
	Model model = reader.read(new FileReader("pom.xml"));
	ApiInfoBuilder builder = new ApiInfoBuilder()
			.title("Person Service Api Documentation")
			.description("Documentation automatically generated")
			.version(model.getVersion())
			.contact(new Contact("Piotr Mińkowski", "piotrminkowski.wordpress.com", "[email protected]"));
	return new Docket(DocumentationType.SWAGGER_2).select()
			.apis(RequestHandlerSelectors.basePackage("pl.piomin.services.boot.controller"))
			.paths(PathSelectors.any()).build()
			.apiInfo(builder.build());
}
 
Example 8
Source File: DeployUtil.java    From kylin 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 9
Source File: MavenRunner.java    From app-runner with MIT License 5 votes vote down vote up
static Model loadPomModel(File pomFile) {
    try {
        MavenXpp3Reader reader = new MavenXpp3Reader();
        try (FileReader pomReader = new FileReader(pomFile)) {
            return reader.read(pomReader);
        }
    } catch (Exception e) {
        throw new ProjectCannotStartException("Error while reading maven meta data", e);
    }
}
 
Example 10
Source File: PomUtils.java    From helidon-build-tools with Apache License 2.0 5 votes vote down vote up
/**
 * Reads the pom model.
 *
 * @param pomFile The pom file.
 * @return The model.
 * @throws RuntimeException on error.
 */
public static Model readPomModel(File pomFile) {
    try {
        try (FileReader fr = new FileReader(pomFile)) {
            MavenXpp3Reader mvnReader = new MavenXpp3Reader();
            return mvnReader.read(fr);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 11
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 12
Source File: Application.java    From Mastering-Spring-Cloud with MIT License 5 votes vote down vote up
@Bean
public Docket api() throws IOException, XmlPullParserException {
	MavenXpp3Reader reader = new MavenXpp3Reader();
	Model model = reader.read(new FileReader("pom.xml"));
	ApiInfoBuilder builder = new ApiInfoBuilder()
			.title("Person Service Api Documentation")
			.description("Documentation automatically generated")
			.version(model.getVersion())
			.contact(new Contact("Piotr Mińkowski", "piotrminkowski.wordpress.com", "[email protected]"));
	return new Docket(DocumentationType.SWAGGER_2).select()
			.apis(RequestHandlerSelectors.basePackage("pl.piomin.services.boot.controller"))
			.paths(PathSelectors.any()).build()
			.apiInfo(builder.build());
}
 
Example 13
Source File: BaseCycloneDxMojo.java    From cyclonedx-maven-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Reads a POM and creates a MavenProject from it.
 * @param in the inputstream to read from
 * @return a MavenProject
 */
private MavenProject readPom(InputStream in) {
    try {
        final MavenXpp3Reader mavenreader = new MavenXpp3Reader();
        try (final InputStreamReader reader = new InputStreamReader(in)) {
            final Model model = mavenreader.read(reader);
            return new MavenProject(model);
        }
    } catch (XmlPullParserException | IOException e) {
        getLog().error("An error occurred attempting to read POM", e);
    }
    return null;
}
 
Example 14
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 15
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 16
Source File: JavaLibraryUtil.java    From developer-studio with Apache License 2.0 4 votes vote down vote up
private static MavenProject getMavenProject(File file) throws Exception {
	MavenXpp3Reader mavenXpp3Reader = new MavenXpp3Reader();
	Model model;
	model = mavenXpp3Reader.read(new FileInputStream(file));
	return new MavenProject(model);
}
 
Example 17
Source File: MavenUtils.java    From developer-studio with Apache License 2.0 4 votes vote down vote up
public static MavenProject getMavenProject(InputStream input) throws Exception {
	MavenXpp3Reader mavenXpp3Reader = new MavenXpp3Reader();
	Model model;
	model = mavenXpp3Reader.read(input);
	return new MavenProject(model);
}
 
Example 18
Source File: AddExtensionMojoTest.java    From quarkus with Apache License 2.0 4 votes vote down vote up
private Model reload() throws IOException, XmlPullParserException {
    MavenXpp3Reader reader = new MavenXpp3Reader();
    try (Reader fr = Files.newBufferedReader(OUTPUT_POM.toPath())) {
        return reader.read(fr);
    }
}
 
Example 19
Source File: ValidateMavenPom.java    From gradle-plugins with MIT License 4 votes vote down vote up
@TaskAction
public void check() throws IOException, XmlPullParserException {

    MavenXpp3Reader reader = new MavenXpp3Reader();
    Model model = reader.read(new FileReader(pomFile.getAsFile().get()));

    if (isEmpty(model.getGroupId())) {
        logError("groupId");
    }
    if (isEmpty(model.getArtifactId())) {
        logError("artifactId");
    }
    if (isEmpty(model.getVersion())) {
        logError("version");
    }

    if (isEmpty(model.getName())) {
        logError("name");
    }
    if (isEmpty(model.getDescription())) {
        logError("description");
    }
    if (isEmpty(model.getUrl())) {
        logError("url");
    }

    if (isEmpty(model.getLicenses())) {
        logError("licenses");
    } else {
        model.getLicenses().forEach(license -> {
            if (isEmpty(license.getName())) {
                logError("license.name");
            }
            if (isEmpty(license.getUrl())) {
                logError("license.url");
            }
        });
    }

    if (isEmpty(model.getDevelopers())) {
        logError("developers");
    } else {
        model.getDevelopers().forEach(developer -> {
            if (isEmpty(developer.getId())) {
                logError("developer.id");
            }
            if (isEmpty(developer.getName())) {
                logError("developer.name");
            }
            if (isEmpty(developer.getEmail())) {
                logError("developer.email");
            }
        });
    }

    if (model.getScm() == null) {
        logError("scm");
    } else {
        if (isEmpty(model.getScm().getConnection())) {
            logError("scm.connection");
        }
        if (isEmpty(model.getScm().getDeveloperConnection())) {
            logError("scm.developerConnection");
        }
        if (isEmpty(model.getScm().getUrl())) {
            logError("scm.url");
        }
    }

    if (errorFound && !getIgnoreFailures()) {
        throw new GradleException();
    }
}
 
Example 20
Source File: Verify.java    From vertx-maven-plugin with Apache License 2.0 4 votes vote down vote up
public static void verifySetup(File pomFile) throws Exception {
    assertNotNull("Unable to find pom.xml", pomFile);
    MavenXpp3Reader xpp3Reader = new MavenXpp3Reader();
    Model model = xpp3Reader.read(new FileInputStream(pomFile));

    MavenProject project = new MavenProject(model);

    Optional<Plugin> vmPlugin = MojoUtils.hasPlugin(project, "io.reactiverse:vertx-maven-plugin");
    assertTrue(vmPlugin.isPresent());

    //Check if the properties have been set correctly
    Properties properties = model.getProperties();
    assertThat(properties.containsKey("vertx.projectVersion")).isTrue();
    assertThat(properties.getProperty("vertx.projectVersion"))
        .isEqualTo(MojoUtils.getVersion("vertx-core-version"));


    assertThat(properties.containsKey("reactiverse-vertx-maven-plugin.projectVersion")).isTrue();
    assertThat(properties.getProperty("reactiverse-vertx-maven-plugin.projectVersion"))
        .isEqualTo(MojoUtils.getVersion("vertx-maven-plugin-version"));

    //Check if the dependencies has been set correctly
    DependencyManagement dependencyManagement = model.getDependencyManagement();
    assertThat(dependencyManagement).isNotNull();
    assertThat(dependencyManagement.getDependencies().isEmpty()).isFalse();

    //Check Vert.x dependencies BOM
    Optional<Dependency> vertxDeps = dependencyManagement.getDependencies().stream()
        .filter(d -> d.getArtifactId().equals("vertx-dependencies")
            && d.getGroupId().equals("io.vertx"))
        .findFirst();

    assertThat(vertxDeps.isPresent()).isTrue();
    assertThat(vertxDeps.get().getVersion()).isEqualTo("${vertx.projectVersion}");

    //Check Vert.x core dependency
    Optional<Dependency> vertxCoreDep = model.getDependencies().stream()
        .filter(d -> d.getArtifactId().equals("vertx-core") && d.getGroupId().equals("io.vertx"))
        .findFirst();
    assertThat(vertxCoreDep.isPresent()).isTrue();
    assertThat(vertxCoreDep.get().getVersion()).isNull();

    //Check Redeploy Configuration
    Plugin vmp = project.getPlugin("io.reactiverse:vertx-maven-plugin");
    Assert.assertNotNull(vmp);
    Xpp3Dom pluginConfig = (Xpp3Dom) vmp.getConfiguration();
    Assert.assertNotNull(pluginConfig);
    String redeploy = pluginConfig.getChild("redeploy").getValue();
    Assert.assertNotNull(redeploy);
    assertTrue(Boolean.valueOf(redeploy));
}