Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

FORGE-842: Add managed plugin support to MavenPluginFacet

  • Loading branch information...
commit f3cff7f491cfe2266f6d2b360cbb20ac313afc06 1 parent b1164cc
@salmon-charles salmon-charles authored committed
View
13 maven-api/src/main/java/org/jboss/forge/maven/MavenPluginFacet.java
@@ -53,6 +53,19 @@ public String getId()
void removePlugin(Dependency dependency);
void updatePlugin(final MavenPlugin plugin);
+
+ List<MavenPlugin> listConfiguredManagedPlugins();
+
+ boolean hasManagedPlugin(Dependency dependency);
+
+ MavenPlugin getManagedPlugin(Dependency dependency);
+
+ void addManagedPlugin(MavenPlugin plugin);
+
+ void removeManagedPlugin(Dependency dependency);
+
+ void updateManagedPlugin(final MavenPlugin plugin);
+
/**
* Add a {@link KnownRepository} to the project build system. This is where dependencies can be found, downloaded,
View
253 project-model-maven-tests/src/test/java/org/jboss/forge/maven/facets/MavenPluginFacetManagedPluginTest.java
@@ -0,0 +1,253 @@
+/*
+ * Copyright 2012 Red Hat, Inc. and/or its affiliates.
+ *
+ * Licensed under the Eclipse Public License version 1.0, available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.jboss.forge.maven.facets;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+
+import javax.inject.Inject;
+
+import org.apache.maven.model.Build;
+import org.apache.maven.model.PluginManagement;
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.forge.maven.MavenCoreFacet;
+import org.jboss.forge.maven.MavenPluginFacet;
+import org.jboss.forge.maven.facets.exceptions.PluginNotFoundException;
+import org.jboss.forge.maven.plugins.MavenPlugin;
+import org.jboss.forge.maven.plugins.MavenPluginAdapter;
+import org.jboss.forge.maven.plugins.MavenPluginBuilder;
+import org.jboss.forge.maven.util.ProjectModelTest;
+import org.jboss.forge.project.Project;
+import org.jboss.forge.project.dependencies.DependencyBuilder;
+import org.jboss.forge.project.facets.DependencyFacet;
+import org.jboss.forge.project.facets.PackagingFacet;
+import org.jboss.forge.project.facets.ResourceFacet;
+import org.jboss.forge.project.services.ProjectFactory;
+import org.jboss.forge.project.services.ResourceFactory;
+import org.jboss.forge.shell.util.ResourceUtil;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/**
+ * @author <a href="mailto:paul.bakker.nl@gmail.com">Paul Bakker</a>
+ */
+@RunWith(Arquillian.class)
+public class MavenPluginFacetManagedPluginTest extends ProjectModelTest
+{
+ @Inject
+ private ProjectFactory projectFactory;
+
+ @Inject
+ private ResourceFactory resourceFactory;
+
+ private static Project testProject;
+
+ private final static int TEST_PROJECT_NBR_OF_MANAGED_PLUGINS = 5;
+
+ @Deployment
+ public static JavaArchive createTestArchive()
+ {
+ return ProjectModelTest.createTestArchive().addAsManifestResource(
+ "META-INF/services/org.jboss.forge.project.dependencies.DependencyResolverProvider");
+ }
+
+ @Before
+ @Override
+ public void before() throws IOException
+ {
+
+ project = null;
+ super.before();
+
+ if (testProject == null)
+ {
+ testProject = projectFactory.findProjectRecursively(ResourceUtil.getContextDirectory(resourceFactory
+ .getResourceFrom(new File("src/test/resources/test-pom-managed"))));
+ }
+ }
+
+ @Test
+ public void testIsInstalled() throws Exception
+ {
+ boolean isInstalled = testProject.hasFacet(MavenPluginFacet.class);
+ assertEquals(true, isInstalled);
+ }
+
+ @Test
+ public void testListManagedPlugins() throws Exception
+ {
+ MavenPluginFacet mavenPluginFacet = testProject.getFacet(MavenPluginFacet.class);
+ List<MavenPlugin> mavenManagedPlugins = mavenPluginFacet.listConfiguredManagedPlugins();
+ assertThat(mavenManagedPlugins.size(), is(TEST_PROJECT_NBR_OF_MANAGED_PLUGINS));
+ }
+
+ @Test
+ public void testAddManagedPlugin()
+ {
+ Project project = getProject();
+ MavenPluginFacet mavenPluginFacet = getProject().getFacet(MavenPluginFacet.class);
+
+ int nrOfManagedPlugins = getNumberOfManagedPlugins(project);
+ MavenPluginBuilder plugin = MavenPluginBuilder.create().setDependency(
+ DependencyBuilder.create().setGroupId("org.apache.maven.plugins").setArtifactId("maven-site-plugin")
+ .setVersion("3.0"));
+
+ mavenPluginFacet.addManagedPlugin(plugin);
+
+ assertThat(getNumberOfManagedPlugins(project), is(nrOfManagedPlugins + 1));
+ }
+
+ private int getNumberOfManagedPlugins(Project project)
+ {
+ MavenCoreFacet mavenCoreFacet = project.getFacet(MavenCoreFacet.class);
+ Build build = mavenCoreFacet.getPOM().getBuild();
+ if (build != null) {
+ PluginManagement pluginManagement = build.getPluginManagement();
+ if (pluginManagement != null) {
+ return pluginManagement.getPlugins().size();
+ }
+ }
+ return 0;
+ }
+
+ @Test
+ public void testHasManagedPlugin()
+ {
+ MavenPluginFacet mavenPluginFacet = testProject.getFacet(MavenPluginFacet.class);
+ boolean hasManagedPlugin = mavenPluginFacet.hasManagedPlugin(DependencyBuilder
+ .create("org.codehaus.mojo:findbugs-maven-plugin"));
+ assertTrue(hasManagedPlugin);
+ }
+
+ @Test
+ public void testHasManagedPluginForDefaultGroupId()
+ {
+ MavenPluginFacet mavenPluginFacet = testProject.getFacet(MavenPluginFacet.class);
+ boolean hasManagedPlugin = mavenPluginFacet.hasManagedPlugin(DependencyBuilder
+ .create("org.apache.maven.plugins:maven-compiler-plugin"));
+ assertTrue(hasManagedPlugin);
+ }
+
+ @Test
+ public void testHasManagedPluginForNullGroupId()
+ {
+ MavenPluginFacet mavenPluginFacet = testProject.getFacet(MavenPluginFacet.class);
+ DependencyBuilder pluginDependency = DependencyBuilder.create().setArtifactId("maven-compiler-plugin");
+ boolean hasManagedPlugin = mavenPluginFacet.hasManagedPlugin(pluginDependency);
+ assertTrue(hasManagedPlugin);
+ }
+
+ @Test
+ public void testHasManagedPluginForEmptyGroupId()
+ {
+ MavenPluginFacet mavenPluginFacet = testProject.getFacet(MavenPluginFacet.class);
+ DependencyBuilder pluginDependency = DependencyBuilder.create().setGroupId("")
+ .setArtifactId("maven-compiler-plugin");
+ boolean hasManagedPlugin = mavenPluginFacet.hasManagedPlugin(pluginDependency);
+ assertTrue(hasManagedPlugin);
+ }
+
+ @Test
+ public void testHasManagedPluginWhenPluginNotInstalled()
+ {
+ MavenPluginFacet mavenPluginFacet = testProject.getFacet(MavenPluginFacet.class);
+ boolean hasManagedPlugin = mavenPluginFacet.hasManagedPlugin(DependencyBuilder.create("test.plugins:fake"));
+ assertFalse(hasManagedPlugin);
+ }
+
+ @Test
+ public void testGetManagedPlugin()
+ {
+ MavenPluginFacet mavenPluginFacet = testProject.getFacet(MavenPluginFacet.class);
+ MavenPlugin plugin = mavenPluginFacet.getManagedPlugin(DependencyBuilder
+ .create("org.codehaus.mojo:findbugs-maven-plugin"));
+ assertNotNull(plugin);
+ assertThat(plugin.getDependency().getArtifactId(), is("findbugs-maven-plugin"));
+ assertThat(plugin.getDependency().getVersion(), is("2.3.2"));
+ }
+
+ @Test
+ @SuppressWarnings("unchecked")
+ public void testGetManagedPluginWhenNoneExistDoesNotThrowException() throws Exception
+ {
+ Project project = createProject(MavenCoreFacet.class, ResourceFacet.class, DependencyFacet.class,
+ PackagingFacet.class);
+ project.getFacet(MavenPluginFacet.class).listConfiguredManagedPlugins();
+ }
+
+ @Test(expected = PluginNotFoundException.class)
+ public void testGetPluginException()
+ {
+ MavenPluginFacet mavenPluginFacet = testProject.getFacet(MavenPluginFacet.class);
+ mavenPluginFacet.getManagedPlugin(DependencyBuilder.create("test.plugins:fake"));
+ }
+
+ @Test
+ public void testRemoveManagedPlugin()
+ {
+ Project project = getProject();
+ MavenPluginFacet mavenPluginFacet = getProject().getFacet(MavenPluginFacet.class);
+ int nrOfManagedPlugins = getNumberOfManagedPlugins(project);
+
+ // FIXME: we should not use the mavenPluginFacet to addManagedPlugin (break unit tests principles)
+ // One way would be to add a pluginManagement to the project (getProject()),
+ // but I can not find out where is defined the pom for this project
+ MavenPluginBuilder plugin = MavenPluginBuilder.create().setDependency(
+ DependencyBuilder.create().setGroupId("org.apache.maven.plugins").setArtifactId("maven-site-plugin")
+ .setVersion("3.0"));
+ mavenPluginFacet.addManagedPlugin(plugin);
+ assertThat(getNumberOfManagedPlugins(project), is(++nrOfManagedPlugins));
+
+ mavenPluginFacet.removeManagedPlugin(DependencyBuilder.create("org.apache.maven.plugins:maven-site-plugin"));
+ assertThat(getNumberOfManagedPlugins(project), is(--nrOfManagedPlugins));
+ }
+
+ @Test
+ public void testAddConfigurationToExistingManagedPlugin()
+ {
+ MavenPluginFacet mavenPluginFacet = testProject.getFacet(MavenPluginFacet.class);
+ MavenPlugin plugin = mavenPluginFacet.getManagedPlugin(DependencyBuilder
+ .create("org.codehaus.mojo:findbugs-maven-plugin"));
+ MavenPluginBuilder pluginBuilder = MavenPluginBuilder.create(plugin);
+
+ pluginBuilder.createConfiguration().createConfigurationElement("xmlOutput").setText("true");
+
+ assertEquals(
+ "<plugin><groupId>org.codehaus.mojo</groupId><artifactId>findbugs-maven-plugin</artifactId><version>2.3.2</version><configuration><xmlOutput>true</xmlOutput></configuration></plugin>",
+ pluginBuilder.toString());
+ }
+
+ @Test
+ public void testAddConfigurationToExistingManagedPluginWithConfig()
+ {
+ MavenPluginFacet mavenPluginFacet = testProject.getFacet(MavenPluginFacet.class);
+ MavenPlugin plugin = mavenPluginFacet
+ .getManagedPlugin(DependencyBuilder.create().setArtifactId("maven-compiler-plugin"));
+ MavenPluginBuilder pluginBuilder = MavenPluginBuilder.create(plugin);
+
+ pluginBuilder.createConfiguration().createConfigurationElement("testelement").setText("test");
+
+ assertEquals(
+ "<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>2.0.2</version><configuration><source>1.6</source><target>1.6</target><testelement>test</testelement></configuration></plugin>",
+ pluginBuilder.toString());
+ }
+
+
+
+}
View
305 project-model-maven-tests/src/test/resources/test-pom-managed/pom.xml
@@ -0,0 +1,305 @@
+<?xml version="1.0"?>
+<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>com.ocpsoft</groupId>
+ <artifactId>socialpm</artifactId>
+ <version>X-SNAPSHOT</version>
+ <packaging>war</packaging>
+ <name>OcpSoft Social Project Management</name>
+ <url>http://ocpsoft.com/scrumshark/</url>
+ <properties>
+ <jbossas.server.manager.version>1.0.3.GA</jbossas.server.manager.version>
+ <slf4j.version>1.5.10</slf4j.version>
+ <junit.version>4.8.1</junit.version>
+ <javaee.spec.version>1.0.0.Beta7</javaee.spec.version>
+ <arquillian.version>1.0.0.Alpha3</arquillian.version>
+ <jbossas.client.version>6.0.0.20100721-M4</jbossas.client.version>
+ <prettyfaces.version>3.0.2-SNAPSHOT</prettyfaces.version>
+ <version>1.0.2.Final</version>
+ <seam.faces.version>3.0.0.Alpha3</seam.faces.version>
+ <hibernate.version>3.5.2-Final</hibernate.version>
+ <jboss-javaee6-spec.version>1.0.0.Final</jboss-javaee6-spec.version>
+ <cdi.version>1.0-SP1</cdi.version>
+ </properties>
+ <dependencyManagement>
+ <dependencies>
+ <dependency>
+ <groupId>org.jboss.spec</groupId>
+ <artifactId>jboss-javaee-6.0</artifactId>
+ <version>${jboss-javaee6-spec.version}</version>
+ <type>pom</type>
+ <scope>import</scope>
+ </dependency>
+ </dependencies>
+ </dependencyManagement>
+ <dependencies>
+ <dependency>
+ <groupId>com.ocpsoft</groupId>
+ <artifactId>prettyfaces-jsf2</artifactId>
+ <version>${prettyfaces.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss.seam.faces</groupId>
+ <artifactId>seam-faces</artifactId>
+ <version>${seam.faces.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.hibernate</groupId>
+ <artifactId>hibernate-entitymanager</artifactId>
+ <version>${hibernate.version}</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss.arquillian</groupId>
+ <artifactId>arquillian-junit</artifactId>
+ <version>${arquillian.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>commons-lang</groupId>
+ <artifactId>commons-lang</artifactId>
+ <version>2.4</version>
+ </dependency>
+ <dependency>
+ <groupId>commons-codec</groupId>
+ <artifactId>commons-codec</artifactId>
+ <version>1.3</version>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>${junit.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss</groupId>
+ <artifactId>test-dependency</artifactId>
+ <version>1.0.0.Final</version>
+ </dependency>
+ </dependencies>
+ <repositories>
+ <repository>
+ <snapshots>
+ <enabled>true</enabled>
+ </snapshots>
+ <id>ocpsoft</id>
+ <name>OcpSoft Releases</name>
+ <url>http://ocpsoft.com/repository/</url>
+ </repository>
+ <repository>
+ <id>jboss-public-repository-group</id>
+ <name>JBoss Public Maven Repository Group</name>
+ <url>https://repository.jboss.org/nexus/content/groups/public/</url>
+ </repository>
+ <repository>
+ <snapshots>
+ <enabled>false</enabled>
+ </snapshots>
+ <id>jboss</id>
+ <name>JBoss Legacy Repo</name>
+ <url>http://repository.jboss.org/maven2/</url>
+ </repository>
+ <repository>
+ <snapshots>
+ <enabled>false</enabled>
+ </snapshots>
+ <id>java.net2</id>
+ <name>Repository hosting the jee6 artifacts</name>
+ <url>http://download.java.net/maven/2</url>
+ </repository>
+ </repositories>
+ <build>
+ <finalName>socialpm</finalName>
+ <pluginManagement>
+ <plugins>
+ <plugin>
+ <groupId>org.eclipse.m2e</groupId>
+ <artifactId>lifecycle-mapping</artifactId>
+ <version>1.0.0</version>
+ <configuration>
+ <lifecycleMappingMetadata>
+ <pluginExecutions>
+ <pluginExecution>
+ <pluginExecutionFilter>
+ <groupId>org.qooxdoo</groupId>
+ <artifactId>qooxdoo-maven-plugin</artifactId>
+ <versionRange>[2.0-RC1,)</versionRange>
+ <goals>
+ <goal>compile</goal>
+ <goal>test-compile</goal>
+ <goal>generate-html</goal>
+ <goal>generate-config</goal>
+ <goal>sdk-unpack</goal>
+ <goal>translation</goal>
+ </goals>
+ </pluginExecutionFilter>
+ <action>
+ <ignore></ignore>
+ </action>
+ </pluginExecution>
+ </pluginExecutions>
+ </lifecycleMappingMetadata>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.qooxdoo</groupId>
+ <artifactId>qooxdoo-maven-plugin</artifactId>
+ <version>2.0-RC1</version>
+ <executions>
+ <execution>
+ <id>sdk-unpack</id>
+ <phase>initialize</phase>
+ <goals>
+ <goal>sdk-unpack</goal>
+ </goals>
+ </execution>
+ <execution>
+ <id>generate-config</id>
+ <phase>generate-sources</phase>
+ <goals>
+ <goal>generate-config</goal>
+ </goals>
+ </execution>
+ <execution>
+ <id>translation</id>
+ <phase>generate-resources</phase>
+ <goals>
+ <goal>translation</goal>
+ </goals>
+ </execution>
+ <execution>
+ <id>generate-html</id>
+ <phase>process-resources</phase>
+ <goals>
+ <goal>generate-html</goal>
+ </goals>
+ </execution>
+ <execution>
+ <id>compile</id>
+ <phase>compile</phase>
+ <goals>
+ <goal>compile</goal>
+ </goals>
+ </execution>
+ <execution>
+ <id>test-compile</id>
+ <phase>test-compile</phase>
+ <goals>
+ <goal>test-compile</goal>
+ </goals>
+ </execution>
+ </executions>
+ <configuration>
+ <useEmbeddedJython>false</useEmbeddedJython>
+ <testUnitBrowser>phantomjs</testUnitBrowser>
+ <testUnitPhantomjsPath>C:\phantomjs-1.9.0\phantomjs.exe</testUnitPhantomjsPath>
+ </configuration>
+ </plugin>
+ <plugin>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>2.0.2</version>
+ <configuration>
+ <source>1.6</source>
+ <target>1.6</target>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>findbugs-maven-plugin</artifactId>
+ <version>2.3.2</version>
+ </plugin>
+ <plugin>
+ <artifactId>maven-war-plugin</artifactId>
+ <version>2.1-beta-1</version>
+ <configuration>
+ <failOnMissingWebXml>false</failOnMissingWebXml>
+ </configuration>
+ </plugin>
+ </plugins>
+ </pluginManagement>
+ <plugins>
+ <plugin>
+ <artifactId>maven-compiler-plugin</artifactId>
+ </plugin>
+ <plugin>
+ <artifactId>maven-war-plugin</artifactId>
+ </plugin>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>findbugs-maven-plugin</artifactId>
+ </plugin>
+ </plugins>
+ </build>
+ <profiles>
+ <profile>
+ <activation>
+ <property>
+ <name>glassfish</name>
+ </property>
+ </activation>
+ <dependencies>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ <version>${slf4j.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-log4j12</artifactId>
+ <version>${slf4j.version}</version>
+ </dependency>
+ </dependencies>
+ </profile>
+ <profile>
+ <id>jbossas-managed-6</id>
+ <dependencies>
+ <dependency>
+ <groupId>org.jboss.arquillian.container</groupId>
+ <artifactId>arquillian-jbossas-managed-6</artifactId>
+ <version>${arquillian.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss.jbossas</groupId>
+ <artifactId>jboss-server-manager</artifactId>
+ <version>${jbossas.server.manager.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss.jbossas</groupId>
+ <artifactId>jboss-as-client</artifactId>
+ <version>${jbossas.client.version}</version>
+ <type>pom</type>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+ </profile>
+ <profile>
+ <id>jbossas-remote-6</id>
+ <build>
+ <testResources>
+ <testResource>
+ <directory>src/test/resources</directory>
+ </testResource>
+ <testResource>
+ <directory>src/test/resources-jbossas</directory>
+ </testResource>
+ </testResources>
+ </build>
+ <dependencies>
+ <dependency>
+ <groupId>org.jboss.arquillian.container</groupId>
+ <artifactId>arquillian-jbossas-remote-6</artifactId>
+ <version>${arquillian.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss.jbossas</groupId>
+ <artifactId>jboss-as-client</artifactId>
+ <version>${jbossas.client.version}</version>
+ <type>pom</type>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+ </profile>
+ </profiles>
+</project>
View
512 project-model-maven/src/main/java/org/jboss/forge/maven/facets/MavenPluginFacetImpl.java
@@ -9,12 +9,15 @@
import java.util.ArrayList;
import java.util.Collections;
+import java.util.Iterator;
import java.util.List;
import javax.enterprise.context.Dependent;
import org.apache.maven.model.Build;
import org.apache.maven.model.Model;
+import org.apache.maven.model.Plugin;
+import org.apache.maven.model.PluginManagement;
import org.apache.maven.model.Repository;
import org.jboss.forge.maven.MavenCoreFacet;
import org.jboss.forge.maven.MavenPluginFacet;
@@ -40,219 +43,332 @@
@Dependent
@Alias("forge.maven.MavenPluginFacet")
@RequiresFacet(MavenCoreFacet.class)
-public class MavenPluginFacetImpl extends BaseFacet implements MavenPluginFacet, Facet {
- private static final String DEFAULT_GROUPID = "org.apache.maven.plugins";
-
- @Override
- public boolean install() {
- return true;
- }
-
- @Override
- public boolean isInstalled() {
- try {
- project.getFacet(MavenCoreFacet.class);
- return true;
- } catch (FacetNotFoundException e) {
- return false;
- }
- }
-
- @Override
- public List<MavenPlugin> listConfiguredPlugins() {
- MavenCoreFacet mavenCoreFacet = project.getFacet(MavenCoreFacet.class);
- Build build = mavenCoreFacet.getPOM().getBuild();
- List<MavenPlugin> plugins = new ArrayList<MavenPlugin>();
- if (build != null) {
- List<org.apache.maven.model.Plugin> pomPlugins = build.getPlugins();
-
- if (pomPlugins != null) {
- for (org.apache.maven.model.Plugin plugin : pomPlugins) {
- MavenPluginAdapter adapter = new MavenPluginAdapter(plugin);
- MavenPluginBuilder pluginBuilder = MavenPluginBuilder
- .create()
- .setDependency(
- DependencyBuilder.create().setGroupId(plugin.getGroupId())
- .setArtifactId(plugin.getArtifactId()).setVersion(plugin.getVersion()))
-
- .setConfiguration(adapter.getConfig());
- for (Execution execution : adapter.listExecutions())
- {
- pluginBuilder.addExecution(execution);
- }
- plugins.add(pluginBuilder);
- }
- }
- }
-
- return plugins;
- }
-
- @Override
- public void addPlugin(final MavenPlugin plugin) {
- MavenCoreFacet mavenCoreFacet = project.getFacet(MavenCoreFacet.class);
- Model pom = mavenCoreFacet.getPOM();
- Build build = pom.getBuild();
- if (build == null) {
- build = new Build();
- }
-
- build.addPlugin(new MavenPluginAdapter(plugin));
- pom.setBuild(build);
- mavenCoreFacet.setPOM(pom);
- }
-
- @Override
- public boolean hasPlugin(final Dependency dependency) {
- try {
- getPlugin(dependency);
- return true;
- } catch (PluginNotFoundException ex) {
- return false;
- }
- }
-
- @Override
- public MavenPlugin getPlugin(final Dependency dependency) {
- String groupId = dependency.getGroupId();
-
- if ((groupId == null) || groupId.equals("")) {
- groupId = DEFAULT_GROUPID;
- }
-
- for (MavenPlugin mavenPlugin : listConfiguredPlugins()) {
- Dependency temp = mavenPlugin.getDependency();
- if (DependencyBuilder.areEquivalent(temp, DependencyBuilder.create(dependency).setGroupId(groupId))) {
- return mavenPlugin;
- }
- }
-
- throw new PluginNotFoundException(groupId, dependency.getArtifactId());
-
- }
-
- @Override
- public void removePlugin(final Dependency dependency) {
- MavenCoreFacet mavenCoreFacet = project.getFacet(MavenCoreFacet.class);
-
- Build build = mavenCoreFacet.getPOM().getBuild();
- if (build != null) {
- List<org.apache.maven.model.Plugin> pomPlugins = build.getPlugins();
- if (pomPlugins != null) {
- for (org.apache.maven.model.Plugin pomPlugin : pomPlugins) {
- Dependency pluginDep = DependencyBuilder.create().setGroupId(pomPlugin.getGroupId())
- .setArtifactId(pomPlugin.getArtifactId());
-
- if (DependencyBuilder.areEquivalent(pluginDep, dependency)) {
- Model pom = mavenCoreFacet.getPOM();
- pom.getBuild().removePlugin(pomPlugin);
- mavenCoreFacet.setPOM(pom);
- }
- }
- }
- }
- }
-
+public class MavenPluginFacetImpl extends BaseFacet implements MavenPluginFacet, Facet
+{
+ private static final String DEFAULT_GROUPID = "org.apache.maven.plugins";
+
@Override
- public void updatePlugin(final MavenPlugin plugin)
+ public boolean install()
+ {
+ return true;
+ }
+
+ @Override
+ public boolean isInstalled()
+ {
+ try
+ {
+ project.getFacet(MavenCoreFacet.class);
+ return true;
+ }
+ catch (FacetNotFoundException e)
+ {
+ return false;
+ }
+ }
+
+ private List<org.apache.maven.model.Plugin> getPluginsPOM(Build build, boolean managedPlugin)
+ {
+ if (build != null)
+ {
+ if (managedPlugin)
+ {
+ PluginManagement pluginManagement = build.getPluginManagement();
+ if (pluginManagement != null)
+ {
+ return pluginManagement.getPlugins();
+ }
+ }
+ else
+ {
+ return build.getPlugins();
+ }
+ }
+ return Collections.emptyList();
+ }
+
+ private List<MavenPlugin> listConfiguredPlugins(boolean managedPlugin)
+ {
+ List<MavenPlugin> plugins = new ArrayList<MavenPlugin>();
+ MavenCoreFacet mavenCoreFacet = project.getFacet(MavenCoreFacet.class);
+ Build build = mavenCoreFacet.getPOM().getBuild();
+ List<org.apache.maven.model.Plugin> pomPlugins = getPluginsPOM(build, managedPlugin);
+ for (org.apache.maven.model.Plugin plugin : pomPlugins)
+ {
+ MavenPluginAdapter adapter = new MavenPluginAdapter(plugin);
+ MavenPluginBuilder pluginBuilder = MavenPluginBuilder
+ .create()
+ .setDependency(
+ DependencyBuilder.create().setGroupId(plugin.getGroupId())
+ .setArtifactId(plugin.getArtifactId()).setVersion(plugin.getVersion()))
+
+ .setConfiguration(adapter.getConfig());
+ for (Execution execution : adapter.listExecutions())
+ {
+ pluginBuilder.addExecution(execution);
+ }
+ plugins.add(pluginBuilder);
+ }
+ return plugins;
+ }
+
+ private void addPlugin(final MavenPlugin plugin, boolean managedPlugin)
{
MavenCoreFacet mavenCoreFacet = project.getFacet(MavenCoreFacet.class);
Model pom = mavenCoreFacet.getPOM();
Build build = pom.getBuild();
- if (build != null)
+ if (build == null)
+ build = new Build();
+ if (managedPlugin)
{
- List<org.apache.maven.model.Plugin> pomPlugins = build.getPlugins();
- if (pomPlugins != null)
+ PluginManagement pluginManagement = build.getPluginManagement();
+ if (pluginManagement == null)
{
- for (org.apache.maven.model.Plugin pomPlugin : pomPlugins)
- {
- Dependency pluginDep = DependencyBuilder.create().setGroupId(pomPlugin.getGroupId())
- .setArtifactId(pomPlugin.getArtifactId());
+ pluginManagement = new PluginManagement();
+ build.setPluginManagement(pluginManagement);
+ }
+ pluginManagement.addPlugin(new MavenPluginAdapter(plugin));
+ }
+ else
+ {
+ build.addPlugin(new MavenPluginAdapter(plugin));
+ }
+ pom.setBuild(build);
+ mavenCoreFacet.setPOM(pom);
+ }
+
+ private MavenPlugin getPlugin(final Dependency dependency, boolean managedPlugin)
+ {
+ String groupId = dependency.getGroupId();
+ groupId = (groupId == null) || groupId.equals("") ? DEFAULT_GROUPID : groupId;
+
+ for (MavenPlugin mavenPlugin : listConfiguredPlugins(managedPlugin))
+ {
+ Dependency temp = mavenPlugin.getDependency();
+ if (DependencyBuilder.areEquivalent(temp, DependencyBuilder.create(dependency).setGroupId(groupId)))
+ {
+ return mavenPlugin;
+ }
+ }
+
+ throw new PluginNotFoundException(groupId, dependency.getArtifactId());
+ }
+
+ public boolean hasPlugin(final Dependency dependency, boolean managedPlugin)
+ {
+ try
+ {
+ getPlugin(dependency, managedPlugin);
+ return true;
+ }
+ catch (PluginNotFoundException ex)
+ {
+ return false;
+ }
+ }
+
+ private void removePlugin(final Dependency dependency, boolean managedPlugin)
+ {
+ MavenCoreFacet mavenCoreFacet = project.getFacet(MavenCoreFacet.class);
+ Model pom = mavenCoreFacet.getPOM();
+ Build build = pom.getBuild();
+ List<org.apache.maven.model.Plugin> pomPlugins = getPluginsPOM(build, managedPlugin);
+ Iterator<Plugin> it = pomPlugins.iterator();
+ while (it.hasNext())
+ {
+ org.apache.maven.model.Plugin pomPlugin = it.next();
+ Dependency pluginDep = DependencyBuilder.create().setGroupId(pomPlugin.getGroupId())
+ .setArtifactId(pomPlugin.getArtifactId());
- if (DependencyBuilder.areEquivalent(pluginDep, plugin.getDependency()))
+ if (DependencyBuilder.areEquivalent(pluginDep, dependency))
+ {
+ it.remove();
+ }
+ }
+ mavenCoreFacet.setPOM(pom);
+ }
+
+ private void updatePlugin(final MavenPlugin plugin, boolean managedPlugin)
+ {
+ MavenCoreFacet mavenCoreFacet = project.getFacet(MavenCoreFacet.class);
+ Model pom = mavenCoreFacet.getPOM();
+ Build build = pom.getBuild();
+ List<org.apache.maven.model.Plugin> pomPlugins = getPluginsPOM(build, managedPlugin);
+ for (org.apache.maven.model.Plugin pomPlugin : pomPlugins)
+ {
+ Dependency pluginDep = DependencyBuilder.create().setGroupId(pomPlugin.getGroupId())
+ .setArtifactId(pomPlugin.getArtifactId());
+
+ if (DependencyBuilder.areEquivalent(pluginDep, plugin.getDependency()))
+ {
+ MavenPluginAdapter adapter = new MavenPluginAdapter(plugin);
+ pomPlugin.setConfiguration(adapter.getConfiguration());
+ mavenCoreFacet.setPOM(pom);
+ break;
+ }
+ }
+ }
+
+ @Override
+ public List<MavenPlugin> listConfiguredPlugins()
+ {
+ return listConfiguredPlugins(false);
+ }
+
+ @Override
+ public List<MavenPlugin> listConfiguredManagedPlugins()
+ {
+ return listConfiguredPlugins(true);
+ }
+
+ @Override
+ public void addPlugin(final MavenPlugin plugin)
+ {
+ addPlugin(plugin, false);
+ }
+
+ @Override
+ public void addManagedPlugin(final MavenPlugin plugin)
+ {
+ addPlugin(plugin, true);
+ }
+
+ @Override
+ public MavenPlugin getPlugin(final Dependency dependency)
+ {
+ return getPlugin(dependency, false);
+ }
+
+ @Override
+ public MavenPlugin getManagedPlugin(final Dependency dependency)
+ {
+ return getPlugin(dependency, true);
+ }
+
+ @Override
+ public boolean hasPlugin(final Dependency dependency)
+ {
+ return hasPlugin(dependency, false);
+ }
+
+ @Override
+ public boolean hasManagedPlugin(final Dependency dependency)
+ {
+ return hasPlugin(dependency, true);
+ }
+
+ @Override
+ public void removePlugin(final Dependency dependency)
+ {
+ removePlugin(dependency, false);
+ }
+
+ @Override
+ public void removeManagedPlugin(final Dependency dependency)
+ {
+ removePlugin(dependency, true);
+ }
+
+ @Override
+ public void updatePlugin(final MavenPlugin plugin)
+ {
+ updatePlugin(plugin, false);
+ }
+
+ @Override
+ public void updateManagedPlugin(final MavenPlugin plugin)
+ {
+ updatePlugin(plugin, true);
+ }
+
+ @Override
+ public void addPluginRepository(final KnownRepository repository)
+ {
+ addPluginRepository(repository.name(), repository.getUrl());
+ }
+
+ @Override
+ public void addPluginRepository(final String name, final String url)
+ {
+ if (!hasPluginRepository(url))
+ {
+ MavenCoreFacet maven = project.getFacet(MavenCoreFacet.class);
+ Model pom = maven.getPOM();
+ Repository repo = new Repository();
+ repo.setId(name);
+ repo.setUrl(url);
+ pom.getPluginRepositories().add(repo);
+ maven.setPOM(pom);
+ }
+ }
+
+ @Override
+ public boolean hasPluginRepository(final KnownRepository repository)
+ {
+ return hasPluginRepository(repository.getUrl());
+ }
+
+ @Override
+ public boolean hasPluginRepository(final String url)
+ {
+ if (url != null)
+ {
+ MavenCoreFacet maven = project.getFacet(MavenCoreFacet.class);
+ Model pom = maven.getPOM();
+ List<Repository> repositories = pom.getPluginRepositories();
+ if (repositories != null)
+ {
+ for (Repository repo : repositories)
+ {
+ if (repo.getUrl().trim().equals(url.trim()))
{
- MavenPluginAdapter adapter = new MavenPluginAdapter(plugin);
- pomPlugin.setConfiguration(adapter.getConfiguration());
- mavenCoreFacet.setPOM(pom);
- break;
+ repositories.remove(repo);
+ maven.setPOM(pom);
+ return true;
}
}
}
}
+ return false;
}
- @Override
- public void addPluginRepository(final KnownRepository repository) {
- addPluginRepository(repository.name(), repository.getUrl());
- }
-
- @Override
- public void addPluginRepository(final String name, final String url) {
- if (!hasPluginRepository(url)) {
- MavenCoreFacet maven = project.getFacet(MavenCoreFacet.class);
- Model pom = maven.getPOM();
- Repository repo = new Repository();
- repo.setId(name);
- repo.setUrl(url);
- pom.getPluginRepositories().add(repo);
- maven.setPOM(pom);
- }
- }
-
- @Override
- public boolean hasPluginRepository(final KnownRepository repository) {
- return hasPluginRepository(repository.getUrl());
- }
-
- @Override
- public boolean hasPluginRepository(final String url) {
- if (url != null) {
- MavenCoreFacet maven = project.getFacet(MavenCoreFacet.class);
- Model pom = maven.getPOM();
- List<Repository> repositories = pom.getPluginRepositories();
- if (repositories != null) {
- for (Repository repo : repositories) {
- if (repo.getUrl().trim().equals(url.trim())) {
- repositories.remove(repo);
- maven.setPOM(pom);
- return true;
- }
- }
- }
- }
- return false;
- }
-
- @Override
- public DependencyRepository removePluginRepository(final String url) {
- if (url != null) {
- MavenCoreFacet maven = project.getFacet(MavenCoreFacet.class);
- Model pom = maven.getPOM();
- List<Repository> repos = pom.getPluginRepositories();
- for (Repository repo : repos) {
- if (repo.getUrl().equals(url.trim())) {
- repos.remove(repo);
- maven.setPOM(pom);
- return new DependencyRepositoryImpl(repo.getId(), repo.getUrl());
- }
- }
- }
- return null;
- }
-
- @Override
- public List<DependencyRepository> getPluginRepositories() {
- List<DependencyRepository> results = new ArrayList<DependencyRepository>();
- MavenCoreFacet maven = project.getFacet(MavenCoreFacet.class);
- Model pom = maven.getPOM();
- List<Repository> repos = pom.getPluginRepositories();
-
- if (repos != null) {
- for (Repository repo : repos) {
- results.add(new DependencyRepositoryImpl(repo.getId(), repo.getUrl()));
+ @Override
+ public DependencyRepository removePluginRepository(final String url)
+ {
+ if (url != null)
+ {
+ MavenCoreFacet maven = project.getFacet(MavenCoreFacet.class);
+ Model pom = maven.getPOM();
+ List<Repository> repos = pom.getPluginRepositories();
+ for (Repository repo : repos)
+ {
+ if (repo.getUrl().equals(url.trim()))
+ {
+ repos.remove(repo);
+ maven.setPOM(pom);
+ return new DependencyRepositoryImpl(repo.getId(), repo.getUrl());
}
- }
- return Collections.unmodifiableList(results);
- }
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public List<DependencyRepository> getPluginRepositories()
+ {
+ List<DependencyRepository> results = new ArrayList<DependencyRepository>();
+ MavenCoreFacet maven = project.getFacet(MavenCoreFacet.class);
+ Model pom = maven.getPOM();
+ List<Repository> repos = pom.getPluginRepositories();
+
+ if (repos != null)
+ {
+ for (Repository repo : repos)
+ {
+ results.add(new DependencyRepositoryImpl(repo.getId(), repo.getUrl()));
+ }
+ }
+ return Collections.unmodifiableList(results);
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.