Java Code Examples for org.apache.maven.project.MavenProject#getPlugin()

The following examples show how to use org.apache.maven.project.MavenProject#getPlugin() . 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: Util.java    From yangtools with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Read current project dependencies and check if it don't grab incorrect
 * artifacts versions which could be in conflict with plugin dependencies.
 *
 * @param project
 *            current project
 * @param repoSystem
 *            repository system
 * @param localRepo
 *            local repository
 * @param remoteRepos
 *            remote repositories
 */
static void checkClasspath(final MavenProject project, final RepositorySystem repoSystem,
        final ArtifactRepository localRepo, final List<ArtifactRepository> remoteRepos) {
    Plugin plugin = project.getPlugin(YangToSourcesMojo.PLUGIN_NAME);
    if (plugin == null) {
        LOG.warn("{} {} not found, dependencies version check skipped", LOG_PREFIX, YangToSourcesMojo.PLUGIN_NAME);
    } else {
        Map<Artifact, Collection<Artifact>> pluginDependencies = new HashMap<>();
        getPluginTransitiveDependencies(plugin, pluginDependencies, repoSystem, localRepo, remoteRepos);

        Set<Artifact> projectDependencies = project.getDependencyArtifacts();
        for (Map.Entry<Artifact, Collection<Artifact>> entry : pluginDependencies.entrySet()) {
            checkArtifact(entry.getKey(), projectDependencies);
            for (Artifact dependency : entry.getValue()) {
                checkArtifact(dependency, projectDependencies);
            }
        }
    }
}
 
Example 2
Source File: MavenUtil.java    From jkube with Eclipse Public License 2.0 4 votes vote down vote up
public static boolean hasPlugin(MavenProject project, String groupId, String artifactId) {
    return project.getPlugin(groupId + ":" + artifactId) != null;
}
 
Example 3
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.version")).isTrue();
    assertThat(properties.getProperty("vertx.version"))
        .isEqualTo(MojoUtils.getVersion("vertx-core-version"));


    assertThat(properties.containsKey("vertx-maven-plugin.version")).isTrue();
    assertThat(properties.getProperty("vertx-maven-plugin.version"))
        .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-stack-depchain")
            && d.getGroupId().equals("io.vertx"))
        .findFirst();

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

    //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");
    assertNotNull(vmp);
    Xpp3Dom pluginConfig = (Xpp3Dom) vmp.getConfiguration();
    assertNotNull(pluginConfig);
    String redeploy = pluginConfig.getChild("redeploy").getValue();
    assertNotNull(redeploy);
    assertTrue(Boolean.valueOf(redeploy));
}
 
Example 4
Source File: SetupMojoTest.java    From vertx-maven-plugin with Apache License 2.0 4 votes vote down vote up
@Test
public void testVerticleCreatPom() throws Exception {

    new File("target/unit/nopom").mkdirs();

    File pomFile = new File("target/unit/nopom/pom.xml");

    String vertxVersion = MojoUtils.getVersion("vertx-core-version");

    Map<String, String> tplContext = new HashMap<>();
    tplContext.put("mProjectGroupId", "com.example.vertx");
    tplContext.put("mProjectArtifactId", "vertx-example");
    tplContext.put("mProjectVersion", "1.0-SNAPSHOT");
    tplContext.put("vertxVersion", vertxVersion);
    tplContext.put("vertxVerticle", "com.example.vertx.MainVerticle");
    tplContext.put("vmpVersion", MojoUtils.getVersion("vertx-maven-plugin-version"));
    SetupTemplateUtils.createPom(tplContext, pomFile);

    assertThat(pomFile).isFile();

    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());
    Plugin vmp = project.getPlugin("io.reactiverse:vertx-maven-plugin");
    assertNotNull(vmp);
    Xpp3Dom pluginConfig = (Xpp3Dom) vmp.getConfiguration();
    assertNotNull(pluginConfig);
    String redeploy = pluginConfig.getChild("redeploy").getValue();
    assertNotNull(redeploy);
    assertTrue(Boolean.valueOf(redeploy));
    Properties projectProps = project.getProperties();
    assertNotNull(projectProps);
    assertFalse(projectProps.isEmpty());
    assertEquals(project.getGroupId(), "com.example.vertx");
    assertEquals(project.getArtifactId(), "vertx-example");
    assertEquals(project.getVersion(), "1.0-SNAPSHOT");
    assertEquals(projectProps.getProperty("vertx.version"), vertxVersion);

    assertThat(projectProps.getProperty("vertx-maven-plugin.version"))
        .contains(MojoUtils.getVersion("vertx-maven-plugin-version"));
    assertEquals(projectProps.getProperty("vertx.verticle"), "com.example.vertx.MainVerticle");
    DependencyManagement dependencyManagement = project.getDependencyManagement();
    assertThat(dependencyManagement).isNotNull();
    assertThat(dependencyManagement.getDependencies()).isNotEmpty();
    Dependency vertxBom = project.getDependencyManagement().getDependencies().get(0);
    assertThat(vertxBom.getGroupId()).isEqualTo("io.vertx");
    assertThat(vertxBom.getArtifactId()).isEqualTo("vertx-stack-depchain");
    assertThat(vertxBom.getVersion()).isEqualTo("${vertx.version}");
    assertThat(vertxBom.getType()).isEqualTo("pom");
    assertThat(vertxBom.getScope()).isEqualTo("import");

    assertThat(project.getDependencies()).isNotEmpty();
    Dependency vertxCore =project.getDependencies().get(0);
    assertThat(vertxCore).isNotNull();
    assertThat(vertxCore.getGroupId()).isEqualTo("io.vertx");
    assertThat(vertxCore.getArtifactId()).isEqualTo("vertx-core");
}
 
Example 5
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));
}
 
Example 6
Source File: SetupMojoTest.java    From vertx-maven-plugin with Apache License 2.0 4 votes vote down vote up
@Test
public void testVerticleCreatPom() throws Exception {

    new File("target/unit/nopom").mkdirs();

    File pomFile = new File("target/unit/nopom/pom.xml");

    String vertxVersion = MojoUtils.getVersion("vertx-core-version");

    Map<String, String> tplContext = new HashMap<>();
    tplContext.put("mProjectGroupId", "com.example.vertx");
    tplContext.put("mProjectArtifactId", "vertx-example");
    tplContext.put("mProjectVersion", "1.0-SNAPSHOT");
    tplContext.put("vertxVersion", vertxVersion);
    tplContext.put("vertxVerticle", "com.example.vertx.MainVerticle");
    tplContext.put("reactiverseVMPVersion", MojoUtils.getVersion("vertx-maven-plugin-version"));
    SetupTemplateUtils.createPom(tplContext, pomFile);

    assertThat(pomFile).isFile();

    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());
    Plugin vmp = project.getPlugin("io.reactiverse:vertx-maven-plugin");
    assertNotNull(vmp);
    Xpp3Dom pluginConfig = (Xpp3Dom) vmp.getConfiguration();
    assertNotNull(pluginConfig);
    String redeploy = pluginConfig.getChild("redeploy").getValue();
    assertNotNull(redeploy);
    assertTrue(Boolean.valueOf(redeploy));
    Properties projectProps = project.getProperties();
    assertNotNull(projectProps);
    assertFalse(projectProps.isEmpty());
    assertEquals(project.getGroupId(), "com.example.vertx");
    assertEquals(project.getArtifactId(), "vertx-example");
    assertEquals(project.getVersion(), "1.0-SNAPSHOT");
    assertEquals(projectProps.getProperty("vertx.version"), vertxVersion);

    assertThat(projectProps.getProperty("vertx-maven-plugin.version"))
        .contains(MojoUtils.getVersion("vertx-maven-plugin-version"));
    assertEquals(projectProps.getProperty("vertx.verticle"), "com.example.vertx.MainVerticle");
    DependencyManagement dependencyManagement = project.getDependencyManagement();
    assertThat(dependencyManagement).isNotNull();
    assertThat(dependencyManagement.getDependencies()).isNotEmpty();
    Dependency vertxBom = project.getDependencyManagement().getDependencies().get(0);
    assertThat(vertxBom.getGroupId()).isEqualTo("io.vertx");
    assertThat(vertxBom.getArtifactId()).isEqualTo("vertx-dependencies");
    assertThat(vertxBom.getVersion()).isEqualTo("${vertx.version}");
    assertThat(vertxBom.getType()).isEqualTo("pom");
    assertThat(vertxBom.getScope()).isEqualTo("import");

    assertThat(project.getDependencies()).isNotEmpty();
    Dependency vertxCore =project.getDependencies().get(0);
    assertThat(vertxCore).isNotNull();
    assertThat(vertxCore.getGroupId()).isEqualTo("io.vertx");
    assertThat(vertxCore.getArtifactId()).isEqualTo("vertx-core");
}
 
Example 7
Source File: ExternalVersionExtension.java    From maven-external-version with Apache License 2.0 4 votes vote down vote up
private void createNewVersionPom( MavenProject mavenProject, Map<String, String> gavVersionMap )
    throws IOException, XmlPullParserException
{
    Reader fileReader = null;
    Writer fileWriter = null;
    try
    {
        fileReader = new FileReader( mavenProject.getFile() );
        Model model = new MavenXpp3Reader().read( fileReader );
        model.setVersion( mavenProject.getVersion() );


        // TODO: this needs to be restructured when other references are updated (dependencies, dep-management, plugins, etc)
        if ( model.getParent() != null )
        {
            String key = buildGavKey( model.getParent().getGroupId(), model.getParent().getArtifactId(),
                                      model.getParent().getVersion() );
            String newVersionForParent = gavVersionMap.get( key );
            if ( newVersionForParent != null )
            {
                model.getParent().setVersion( newVersionForParent );
            }
        }
        
        Plugin plugin = mavenProject.getPlugin( "org.apache.maven.plugins:maven-external-version-plugin" );
        // now we are going to wedge in the config
        Xpp3Dom pluginConfiguration = (Xpp3Dom) plugin.getConfiguration();
        
        File newPom = createFileFromConfiguration( mavenProject, pluginConfiguration ); 
        logger.debug( ExternalVersionExtension.class.getSimpleName() + ": using new pom file => " + newPom );
        fileWriter = new FileWriter( newPom );
        new MavenXpp3Writer().write( fileWriter, model );

        mavenProject.setFile( newPom );
    }
    finally
    {
        IOUtil.close( fileReader );
        IOUtil.close( fileWriter );
    }


}
 
Example 8
Source File: AbstracMavenProjectConfigurator.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 4 votes vote down vote up
private Plugin getEclipsePlugin(MavenProject mavenProject) {
  return mavenProject.getPlugin(MAVEN_ECLIPSE_PLUGIN_ID);
}
 
Example 9
Source File: AbstracMavenProjectConfigurator.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 4 votes vote down vote up
protected Plugin getGwtMavenPlugin1(MavenProject mavenProject) {
  return mavenProject.getPlugin(MAVEN_GWT_PLUGIN_ID1);
}
 
Example 10
Source File: AbstracMavenProjectConfigurator.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 4 votes vote down vote up
protected Plugin getGwtMavenPlugin2(MavenProject mavenProject) {
  return mavenProject.getPlugin(MAVEN_GWT_PLUGIN_ID2);
}
 
Example 11
Source File: AbstracMavenProjectConfigurator.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Get the War plugin.
 *
 * @param mavenProject
 * @return the war plugin.
 */
protected Plugin getWarPlugin(MavenProject mavenProject) {
  return mavenProject.getPlugin(MAVEN_WAR_PLUGIN);
}
 
Example 12
Source File: AbstracMavenProjectConfigurator.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Returns true if the first GWT Maven plugin exists.
 *
 * @param mavenProject
 * @return true if the first Maven plugin was used
 */
protected boolean isGwtMavenPlugin1(MavenProject mavenProject) {
  return mavenProject.getPlugin(MAVEN_GWT_PLUGIN_ID1) != null;
}
 
Example 13
Source File: AbstracMavenProjectConfigurator.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Returns true if the second GWT Maven plugin exists.
 *
 * @param mavenProject
 * @return true if the second GWT Maven plugin exists.
 */
protected boolean isGwtMavenPlugin2(MavenProject mavenProject) {
  return mavenProject.getPlugin(MAVEN_GWT_PLUGIN_ID2) != null;
}
 
Example 14
Source File: MavenProjectUtil.java    From ci.maven with Apache License 2.0 2 votes vote down vote up
/**
 * Get an execution of a plugin
 * @param project
 * @param pluginKey
 * @param goal
 * @return the execution object
 * @throws PluginScenarioException
 */
public static PluginExecution getPluginGoalExecution(MavenProject project, String pluginKey, String goal) throws PluginScenarioException {
    Plugin plugin = project.getPlugin(pluginKey);
    return getPluginGoalExecution(plugin, goal);
}
 
Example 15
Source File: MavenProjectUtil.java    From ci.maven with Apache License 2.0 2 votes vote down vote up
/**
 * Get major plugin version
 * @param project
 * @param pluginKey
 * @return the major plugin version
 * @throws PluginScenarioException
 */
public static int getMajorPluginVersion(MavenProject project, String pluginKey) throws PluginScenarioException {
    Plugin plugin = project.getPlugin(pluginKey);
    return Character.getNumericValue(plugin.getVersion().charAt(0));
}