Example usage for org.apache.maven.project MavenProject MavenProject

List of usage examples for org.apache.maven.project MavenProject MavenProject

Introduction

In this page you can find the example usage for org.apache.maven.project MavenProject MavenProject.

Prototype

public MavenProject(MavenProject project) 

Source Link

Usage

From source file:governance.plugin.GovernanceMojo.java

License:Apache License

public void process(File file) throws MojoExecutionException {
    if (file.isFile() && file.getName().equals("pom.xml")) {
        pomFileCount++;/*from  w w  w .java  2  s  .c  o  m*/
        getLog().debug("Processing " + file.getAbsoluteFile());

        Model model = PomParser.parse(file);
        if (model == null) {
            throw new MojoExecutionException("Error while processing  " + file.getAbsoluteFile());
        }

        // Creating a module representing the artifact generated by pom file
        MavenProject project = new MavenProject(model);
        moduleCreator
                .create(new String[] { project.getArtifactId(), project.getVersion(), file.getAbsolutePath() });

        String moduleAbsolutPath = moduleCreator.getAbsoluteArtifactResourcePath(
                new String[] { project.getArtifactId(), project.getVersion() });

        // Creating dependency artifacts to represent dependencies of the module
        List<Dependency> dependencies = project.getDependencies();
        for (Dependency dependency : dependencies) {
            dependencyCreator.create(new String[] { dependency.getGroupId(), dependency.getArtifactId(),
                    dependency.getVersion() });

            String dependencyAbsolutePath = dependencyCreator.getAbsoluteArtifactResourcePath(
                    new String[] { dependency.getGroupId(), dependency.getArtifactId() });

            // Adding association for each dependency with the module
            addAssociation(moduleAbsolutPath, dependencyAbsolutePath, "depends");
        }
    }
}

From source file:governance.plugin.ServiceGovernanceMojo.java

License:Apache License

public void linkServiceWithModule(Map<String, String> parameters, File file) throws MojoExecutionException {

    File currentPOM = findNearestPOMFile(file);
    if (currentPOM == null) {
        throw new MojoExecutionException(
                "Cannot find a POM related to this module. [file=" + file.getAbsolutePath() + "]");
    }//from  w w  w.j av  a  2 s.  c o  m

    Model model = PomParser.parse(currentPOM);
    if (model == null) {
        throw new MojoExecutionException("Error while processing  " + currentPOM.getAbsoluteFile());
    }

    // Creating a module representing the artifact generated by pom file
    MavenProject project = new MavenProject(model);

    String moduleAbsolutPath = moduleCreator
            .getAbsoluteArtifactResourcePath(new String[] { project.getArtifactId(), project.getVersion() });

    String dependencyAbsolutePath = serviceCreator.getAbsoluteArtifactResourcePath(
            new String[] { parameters.get("name"), parameters.get("namespace") });

    if (!artifactCreatorUtil.isArtifactExisting(moduleAbsolutPath)) {
        moduleCreator.create(
                new String[] { project.getArtifactId(), project.getVersion(), currentPOM.getAbsolutePath() });
    }

    //Adding association for each service with the module
    addAssociation(moduleAbsolutPath, dependencyAbsolutePath, "depends");
    addAssociation(dependencyAbsolutePath, moduleAbsolutPath, "usedBy");
}

From source file:governance.plugin.WebAppGovernanceMojo.java

License:Apache License

public void linkServiceWithModule(Map<String, String> parameters, File file) throws MojoExecutionException {

    File currentPOM = findNearestPOMFile(file);
    if (currentPOM == null) {
        throw new MojoExecutionException(
                "Cannot find a POM related to this module. [file=" + file.getAbsolutePath() + "]");
    }/*from ww w .  j  ava  2 s . com*/

    Model model = PomParser.parse(currentPOM);
    if (model == null) {
        throw new MojoExecutionException("Error while processing  " + currentPOM.getAbsoluteFile());
    }

    // Creating a module representing the artifact generated by pom file
    MavenProject project = new MavenProject(model);

    String moduleAbsolutPath = moduleCreator
            .getAbsoluteArtifactResourcePath(new String[] { project.getArtifactId(), project.getVersion() });

    String dependencyAbsolutePath = webApplicationCreator.getAbsoluteArtifactResourcePath(
            new String[] { parameters.get("name"), parameters.get("namespace") });

    if (!artifactCreatorUtil.isArtifactExisting(moduleAbsolutPath)) {
        moduleCreator.create(
                new String[] { project.getArtifactId(), project.getVersion(), currentPOM.getAbsolutePath() });
    }

    //Adding association for each service with the module
    addAssociation(moduleAbsolutPath, dependencyAbsolutePath, "depends");
    addAssociation(dependencyAbsolutePath, moduleAbsolutPath, "usedBy");
}

From source file:info.mikaelsvensson.devtools.sitesearch.SiteSearchPlugin.java

License:Apache License

private MavenProject loadPomFile(final File pomFile) throws IOException, XmlPullParserException {
    MavenXpp3Reader mavenXpp3Reader = new MavenXpp3Reader();
    Model model = mavenXpp3Reader.read(new FileReader(pomFile));
    model.setPomFile(pomFile);//from   w w  w  .  ja  v a  2 s  . c  o m
    return new MavenProject(model);
}

From source file:io.fabric8.vertx.maven.plugin.mojos.SetupMojo.java

License:Apache License

@Override
public void execute() throws MojoExecutionException, MojoFailureException {

    File pomFile = project.getFile();

    Model model;/*from   w w  w.j a  v a 2  s . c o  m*/

    //Create pom.xml if not
    if (pomFile == null || !pomFile.isFile()) {
        String workingdDir = System.getProperty("user.dir");
        getLog().info("No pom.xml found, creating it in " + workingdDir);
        pomFile = new File(workingdDir, "pom.xml");
        Prompter prompter = new Prompter();
        prompter.startInteraction();
        try {

            if (projectGroupId == null) {
                projectGroupId = prompter.prompt("Define value for property 'projectGroupId'",
                        "com.example.vertx");
            }

            // If the user does not specify the artifactId, we switch to the interactive mode.
            if (projectArtifactId == null) {
                projectArtifactId = prompter.prompt("Define value for property 'projectArtifactId'",
                        "vertx-example");

                // Ask for version only if we asked for the artifactId
                projectVersion = prompter.prompt("Define value for property 'projectVersion'", projectVersion);

                // Ask for maven version if not set
                if (vertxVersion == null) {
                    vertxVersion = prompter.prompt("Define value for property 'vertx.projectVersion'",
                            MojoUtils.getVersion("vertx-core-version"));
                }

                if (verticle == null) {
                    verticle = prompter.prompt("Define value for property 'vertx.verticle'",
                            projectGroupId + ".MainVerticle");
                }
            }

            if (verticle != null && verticle.endsWith(".java")) {
                verticle = verticle.substring(0, verticle.length() - ".java".length());
            }

            Map<String, String> context = new HashMap<>();
            context.put("mProjectGroupId", projectGroupId);
            context.put("mProjectArtifactId", projectArtifactId);
            context.put("mProjectVersion", projectVersion);
            context.put("vertxVersion",
                    vertxVersion != null ? vertxVersion : MojoUtils.getVersion("vertx-core-version"));

            context.put("vertxVerticle", verticle);
            context.put("fabric8VMPVersion", MojoUtils.getVersion(VERTX_MAVEN_PLUGIN_VERSION_PROPERTY));

            SetupTemplateUtils.createPom(context, pomFile);

            //The project should be recreated and set with right model
            MavenXpp3Reader xpp3Reader = new MavenXpp3Reader();

            model = xpp3Reader.read(new FileInputStream(pomFile));
        } catch (Exception e) {
            throw new MojoExecutionException("Error while setup of vertx-maven-plugin", e);
        } finally {
            prompter.endInteraction();
        }

        project = new MavenProject(model);
        project.setPomFile(pomFile);
        project.setOriginalModel(model); // the current model is the original model as well

        // Add dependencies
        if (addDependencies(model)) {
            save(pomFile, model);
        }
    }

    //We should get cloned of the OriginalModel, as project.getModel will return effective model
    model = project.getOriginalModel().clone();

    createDirectories();
    SetupTemplateUtils.createVerticle(project, verticle, getLog());

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

    if (!vmPlugin.isPresent()) {

        //Set  a property at maven project level for vert.x  and vert.x maven plugin versions
        model.getProperties().putIfAbsent("fabric8-vertx-maven-plugin.projectVersion",
                MojoUtils.getVersion(VERTX_MAVEN_PLUGIN_VERSION_PROPERTY));

        vertxVersion = vertxVersion == null ? MojoUtils.getVersion("vertx-core-version") : vertxVersion;

        model.getProperties().putIfAbsent("vertx.projectVersion", vertxVersion);
        if (!Strings.isNullOrEmpty(verticle)) {
            if (verticle.endsWith(".java")) {
                verticle = verticle.substring(0, verticle.length() - ".java".length());
            }
            model.getProperties().putIfAbsent("vertx.verticle", verticle);
        }

        //Add Vert.x BOM
        addVertxBom(model);

        //Add Vert.x Core Dependency
        addVertxDependencies(model);
        // Add other dependencies
        addDependencies(model);

        Plugin vertxMavenPlugin = plugin(PLUGIN_GROUPID, PLUGIN_ARTIFACTID,
                "${fabric8-vertx-maven-plugin.projectVersion}");

        if (isParentPom(model)) {
            if (model.getBuild().getPluginManagement() != null) {
                if (model.getBuild().getPluginManagement().getPlugins() == null) {
                    model.getBuild().getPluginManagement().setPlugins(new ArrayList<>());
                }
                model.getBuild().getPluginManagement().getPlugins().add(vertxMavenPlugin);
            }
            //strip the vertxVersion off
            vertxMavenPlugin = plugin(PLUGIN_GROUPID, PLUGIN_ARTIFACTID);
        } else {
            vertxMavenPlugin = plugin(PLUGIN_GROUPID, PLUGIN_ARTIFACTID,
                    "${fabric8-vertx-maven-plugin.projectVersion}");
        }

        PluginExecution pluginExec = new PluginExecution();
        pluginExec.addGoal("initialize");
        pluginExec.addGoal("package");
        pluginExec.setId("vmp-init-package");
        vertxMavenPlugin.addExecution(pluginExec);

        //Plugin Configuration
        vertxMavenPlugin.setConfiguration(configuration(element("redeploy", "true")));

        Build build = model.getBuild();

        if (build == null) {
            build = new Build();
            model.setBuild(build);
        }

        if (build.getPlugins() == null) {
            build.setPlugins(new ArrayList<>());
        }

        build.getPlugins().add(vertxMavenPlugin);

        save(pomFile, model);
    }
}

From source file:io.fabric8.vertx.maven.plugin.Verify.java

License:Apache License

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.fabric8:vertx-maven-plugin");
    assertTrue(vmPlugin.isPresent());// ww  w. j  a  v  a2s  .  c  om

    //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("fabric8-vertx-maven-plugin.projectVersion")).isTrue();
    assertThat(properties.getProperty("fabric8-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.fabric8: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));
}

From source file:io.fabric8.vertx.maven.plugin.Verify.java

License:Apache License

public static void verifySetupWithVersion(File pomFile) throws Exception {
    MavenXpp3Reader xpp3Reader = new MavenXpp3Reader();
    Model model = xpp3Reader.read(new FileInputStream(pomFile));

    MavenProject project = new MavenProject(model);
    Properties projectProps = project.getProperties();
    Assert.assertNotNull(projectProps);/*w  ww.j av a  2s  . c  o m*/
    assertFalse(projectProps.isEmpty());
    assertEquals(projectProps.getProperty("vertx.projectVersion"), "3.4.0");
}

From source file:io.reactiverse.vertx.maven.plugin.mojos.SetupMojo.java

License:Apache License

@Override
public void execute() throws MojoExecutionException, MojoFailureException {

    File pomFile = project.getFile();

    Model model;//from www  .  j a v  a  2  s.c  om

    //Create pom.xml if not
    if (pomFile == null || !pomFile.isFile()) {
        String workingdDir = System.getProperty("user.dir");
        getLog().info("No pom.xml found, creating it in " + workingdDir);
        pomFile = new File(workingdDir, "pom.xml");
        try {

            if (projectGroupId == null) {
                projectGroupId = prompter.promptWithDefaultValue("Set the project groupId", "io.vertx.example");
            }

            // If the user does not specify the artifactId, we switch to the interactive mode.
            if (projectArtifactId == null) {
                projectArtifactId = prompter.promptWithDefaultValue("Set the project artifactId",
                        "my-vertx-project");
                // Ask for version only if we asked for the artifactId
                projectVersion = prompter.promptWithDefaultValue("Set the project version", "1.0-SNAPSHOT");

                // Ask for maven version if not set
                if (vertxVersion == null) {
                    vertxVersion = prompter.promptWithDefaultValue("Set the Vert.x version",
                            MojoUtils.getVersion("vertx-core-version"));
                }

                if (verticle == null) {
                    verticle = prompter.promptWithDefaultValue("Set the vertcile class name",
                            projectGroupId + ".MainVerticle");
                }
            }

            if (verticle != null && verticle.endsWith(".java")) {
                verticle = verticle.substring(0, verticle.length() - ".java".length());
            }

            Map<String, String> context = new HashMap<>();
            context.put("mProjectGroupId", projectGroupId);
            context.put("mProjectArtifactId", projectArtifactId);
            context.put("mProjectVersion", projectVersion);
            context.put("vertxVersion",
                    vertxVersion != null ? vertxVersion : MojoUtils.getVersion("vertx-core-version"));

            context.put("vertxVerticle", verticle);
            context.put("reactiverseVMPVersion", MojoUtils.getVersion(VERTX_MAVEN_PLUGIN_VERSION_PROPERTY));

            SetupTemplateUtils.createPom(context, pomFile);

            //The project should be recreated and set with right model
            MavenXpp3Reader xpp3Reader = new MavenXpp3Reader();

            model = xpp3Reader.read(new FileInputStream(pomFile));
        } catch (Exception e) {
            throw new MojoExecutionException("Error while setup of vertx-maven-plugin", e);
        }

        project = new MavenProject(model);
        project.setPomFile(pomFile);
        project.setOriginalModel(model); // the current model is the original model as well

        // Add dependencies
        if (addDependencies(model)) {
            save(pomFile, model);
        }
    }

    //We should get cloned of the OriginalModel, as project.getModel will return effective model
    model = project.getOriginalModel().clone();

    createDirectories();
    SetupTemplateUtils.createVerticle(project, verticle, getLog());

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

    if (!vmPlugin.isPresent()) {

        //Set  a property at maven project level for vert.x  and vert.x maven plugin versions
        model.getProperties().putIfAbsent("reactiverse-vertx-maven-plugin.projectVersion",
                MojoUtils.getVersion(VERTX_MAVEN_PLUGIN_VERSION_PROPERTY));

        vertxVersion = vertxVersion == null ? MojoUtils.getVersion("vertx-core-version") : vertxVersion;

        model.getProperties().putIfAbsent("vertx.projectVersion", vertxVersion);
        if (!Strings.isNullOrEmpty(verticle)) {
            if (verticle.endsWith(".java")) {
                verticle = verticle.substring(0, verticle.length() - ".java".length());
            }
            model.getProperties().putIfAbsent("vertx.verticle", verticle);
        }

        //Add Vert.x BOM
        addVertxBom(model);

        //Add Vert.x Core Dependency
        addVertxDependencies(model);
        // Add other dependencies
        addDependencies(model);

        Plugin vertxMavenPlugin = plugin(PLUGIN_GROUPID, PLUGIN_ARTIFACTID,
                "${reactiverse-vertx-maven-plugin.projectVersion}");

        if (isParentPom(model)) {
            if (model.getBuild().getPluginManagement() != null) {
                if (model.getBuild().getPluginManagement().getPlugins() == null) {
                    model.getBuild().getPluginManagement().setPlugins(new ArrayList<>());
                }
                model.getBuild().getPluginManagement().getPlugins().add(vertxMavenPlugin);
            }
            //strip the vertxVersion off
            vertxMavenPlugin = plugin(PLUGIN_GROUPID, PLUGIN_ARTIFACTID);
        } else {
            vertxMavenPlugin = plugin(PLUGIN_GROUPID, PLUGIN_ARTIFACTID,
                    "${reactiverse-vertx-maven-plugin.projectVersion}");
        }

        PluginExecution pluginExec = new PluginExecution();
        pluginExec.addGoal("initialize");
        pluginExec.addGoal("package");
        pluginExec.setId("vmp-init-package");
        vertxMavenPlugin.addExecution(pluginExec);

        //Plugin Configuration
        vertxMavenPlugin.setConfiguration(configuration(element("redeploy", "true")));

        Build build = model.getBuild();

        if (build == null) {
            build = new Build();
            model.setBuild(build);
        }

        if (build.getPlugins() == null) {
            build.setPlugins(new ArrayList<>());
        }

        build.getPlugins().add(vertxMavenPlugin);

        save(pomFile, model);
    }
}

From source file:io.reactiverse.vertx.maven.plugin.Verify.java

License:Apache License

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());/*from  ww  w  .  j a  v  a  2 s.c om*/

    //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));
}

From source file:io.sundr.maven.AbstractSundrioMojo.java

License:Apache License

MavenProject readProject(File pomFile) throws IOException {
    MavenXpp3Reader mavenReader = new MavenXpp3Reader();
    FileReader fileReader = null;
    try {/*from  w ww . ja va  2 s  . c  om*/
        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();
        }
    }
}