How can I get all plugins from java code including default plugins? Maven

303 Views Asked by At

When I build an effective model using help:effective-pom I get such plugins

<plugins>
        <plugin>
          <artifactId>maven-checkstyle-plugin</artifactId>
          <version>2.15</version>
          <configuration>
            <configLocation>checkstyle.xml</configLocation>
            <includeTests>true</includeTests>
            <linkXRef>false</linkXRef>
          </configuration>
        </plugin>
        <plugin>
          <artifactId>maven-pmd-plugin</artifactId>
          <version>3.6</version>
        </plugin>
        <plugin>
          <groupId>some-group-id</groupId>
          <artifactId>build-express</artifactId>
          <version>2.2.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.6.1</version>
          <configuration>
            <source>1.8</source>
            <target>1.8</target>
          </configuration>
        </plugin>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.8</version>
          <dependencies>
            <dependency>
              <groupId>org.apache.ant</groupId>
              <artifactId>ant</artifactId>
              <version>1.7.1</version>
              <scope>compile</scope>
            </dependency>
            <dependency>
              <groupId>jdom</groupId>
              <artifactId>jdom</artifactId>
              <version>0.7</version>
              <scope>compile</scope>
            </dependency>
            <dependency>
              <groupId>commons-logging</groupId>
              <artifactId>commons-logging</artifactId>
              <version>1.1</version>
              <scope>compile</scope>
            </dependency>
            <dependency>
              <groupId>com.sun</groupId>
              <artifactId>tools</artifactId>
              <version>1.5.0</version>
              <scope>system</scope>
              <systemPath>C:\Tools\java\jdk1.8.0_74\jre/../lib/tools.jar</systemPath>
            </dependency>
          </dependencies>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
          <configuration>
            <archive>
              <manifestEntries>
                <Built-By>Apache Maven</Built-By>
              </manifestEntries>
              <addMavenDescriptor>false</addMavenDescriptor>
            </archive>
          </configuration>
        </plugin>
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>xml-maven-plugin</artifactId>
          <version>1.0.1</version>
          <executions>
            <execution>
              <goals>
                <goal>validate</goal>
              </goals>
              <configuration>
                <validationSets>
                  <validationSet>
                    <dir>dish_common/install</dir>
                    <includes>
                      <include>**/*.xml</include>
                    </includes>
                  </validationSet>
                  <validationSet>
                    <dir>dish_common/settings</dir>
                    <includes>
                      <include>**/*.xml</include>
                    </includes>
                  </validationSet>
                </validationSets>
              </configuration>
            </execution>
          </executions>
          <inherited>false</inherited>
          <configuration>
            <validationSets>
              <validationSet>
                <dir>dish_common/install</dir>
                <includes>
                  <include>**/*.xml</include>
                </includes>
              </validationSet>
              <validationSet>
                <dir>dish_common/settings</dir>
                <includes>
                  <include>**/*.xml</include>
                </includes>
              </validationSet>
            </validationSets>
          </configuration>
        </plugin>
        <plugin>
          <groupId>some-group-id</groupId>
          <artifactId>some-our-plugin</artifactId>
          <version>3.7.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>2.5</version>
          <executions>
            <execution>
              <id>default-clean</id>
              <phase>clean</phase>
              <goals>
                <goal>clean</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.4</version>
          <executions>
            <execution>
              <id>default-install</id>
              <phase>install</phase>
              <goals>
                <goal>install</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.7</version>
          <executions>
            <execution>
              <id>default-deploy</id>
              <phase>deploy</phase>
              <goals>
                <goal>deploy</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.3</version>
          <executions>
            <execution>
              <id>default-site</id>
              <phase>site</phase>
              <goals>
                <goal>site</goal>
              </goals>
              <configuration>
                <outputDirectory>C:\Workspace\Projects\Dish\target\site</outputDirectory>
                <reportPlugins>
                  <reportPlugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-project-info-reports-plugin</artifactId>
                  </reportPlugin>
                </reportPlugins>
              </configuration>
            </execution>
            <execution>
              <id>default-deploy</id>
              <phase>site-deploy</phase>
              <goals>
                <goal>deploy</goal>
              </goals>
              <configuration>
                <outputDirectory>C:\Workspace\Projects\Dish\target\site</outputDirectory>
                <reportPlugins>
                  <reportPlugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-project-info-reports-plugin</artifactId>
                  </reportPlugin>
                </reportPlugins>
              </configuration>
            </execution>
          </executions>
          <configuration>
            <outputDirectory>C:\Workspace\Projects\Dish\target\site</outputDirectory>
            <reportPlugins>
              <reportPlugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-project-info-reports-plugin</artifactId>
              </reportPlugin>
            </reportPlugins>
          </configuration>
        </plugin>
      </plugins>
    </build>

(replaced GAV of our plugins with an abbreviation)

But when I get effective model from java code I'm not getting the last 4 plugins.

Java code

ModelBuilder modelBuilder =  new DefaultModelBuilderFactory().newInstance();

ModelResolver modelResolver = ModelResolverFactory.create(aetherArtifactResolver, remoteRepositoryManager, mavenProject);

DefaultModelBuildingRequest modulebr = new DefaultModelBuildingRequest();
modulebr.setPomFile(projectPom);
modulebr.setModelResolver(modelResolver);
// modulebr.setTwoPhaseBuilding(false); 
// modulebr.setProcessPlugins(true); //I try to use these settings but it also not help
modulebr.setUserProperties(props);

Model modelEffectiveModel = modelBuilder.build(modelbr).getEffectiveModel();

List<Plugin> plugins = modelEffectiveModel.getBuild().getPlugins();
public static ModelResolver create(
            ArtifactResolver resolver, RemoteRepositoryManager remoteRepositoryManager, MavenProject project)
            throws EffectivePomBuildingException {
        try {
            ProjectBuildingRequest projectBuildingRequest = project.getProjectBuildingRequest();

            Class c = Class.forName("org.apache.maven.repository.internal.DefaultModelResolver");
            Constructor ct = c.getDeclaredConstructor(RepositorySystemSession.class
                    , RequestTrace.class, String.class
                    , ArtifactResolver.class, VersionRangeResolver.class, RemoteRepositoryManager.class
                    , List.class);
            ct.setAccessible(true);
            return (org.apache.maven.model.resolution.ModelResolver) ct.newInstance(
                    projectBuildingRequest.getRepositorySession()
                    , null, null, resolver, new DefaultVersionRangeResolver(), remoteRepositoryManager
                    , project.getRemoteProjectRepositories());
        } catch (Exception e) {
            throw new BuildingException("Error instantiating DefaultModelResolver for parsing", e);
        }

Help please Why using the getEffectiveModel() method I don't get: maven-clean-plugin, maven-install-plugin, maven-deploy-plugin, maven-site-plugin. And how can I get all the plugins from an effective pom?

1

There are 1 best solutions below

0
On

The goal was to build an effective model and download to the folder all the dependencies, plugins (including all default plugins) and their transitives (by GAV) used for assembly, so that then to assemble the project in offline mode, roughly speaking (here is a piece of the entire implementation).

When building an effective model using ModelBuilder, it was not possible to get the default plugins of the project (such as maven-install-plugin, maven-clean-plugin, and so on). The solution is to change the construction of the model using ProjectBuilder with the following settings:

 ProjectBuildingRequest projectBuildingRequest = new DefaultProjectBuildingRequest(
            mavenSession.getProjectBuildingRequest());
    projectBuildingRequest.setLocalRepository(mavenSession.getLocalRepository());
    projectBuildingRequest.setProject(null);
    projectBuildingRequest.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
    projectBuildingRequest.setResolveDependencies(false);
    projectBuildingRequest.setUserProperties(initAndGetProperties(rootProjectPom, mavenProject));
  Model modelEffectiveModel = projectBuilder.build(pomModel.getModel().getPomFile(), request)
                .getProject().getModel(); 
  List<Plugin> plugins = model.getBuild().getPlugins();

After that, it was possible to get all the plugins of the project, including the default ones