如何从 java 代码中获取所有插件,包括默认插件?马文

Posted

技术标签:

【中文标题】如何从 java 代码中获取所有插件,包括默认插件?马文【英文标题】:How can I get all plugins from java code including default plugins? Maven 【发布时间】:2021-11-25 19:51:33 【问题描述】:

当我使用 help:effective-pom 构建有效模型时,我会得到这样的插件

<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>

将我们插件的 GAV 替换为缩写

但是当我从 java 代码中获得有效模型时,我没有得到最后 4 个插件。

Java 代码

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

请帮忙 为什么使用我没有得到的 getEffectiveModel() 方法:maven-clean-plugin、maven-install-plugin、maven-deploy-plugin、maven-site-plugin。 以及如何从有效的 pom 中获取所有插件?

【问题讨论】:

也许source code for the help plugin 有指向正确方向的线索。 是的,我已经尝试过查看插件代码但是如果我使用 maven-helper-plugin 中的逻辑并使用 Project Builder 构建有效的模型,它也无济于事 看起来你“只是”负责依赖关系的解析器部分......但不是用于启动插件的部分,这些插件成为有效模型的一部分......问题是进入我脑海的是:你为什么喜欢这样做?你想解决什么样的问题? 【参考方案1】:

目标是建立一个有效的模型并将所有用于组装的依赖项、插件(包括所有默认插件)及其传递对象(由 GAV)下载到文件夹中,以便在离线模式下组装项目,大致说话(这里是整个实现的一部分)。

使用模型构建器构建有效模型时,无法获取项目的默认插件(如 maven-install-plugin、maven-clean-plugin 等)。解决方案是使用 ProjectBuilder 通过以下设置更改模型的构造:

 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();

之后就可以拿到项目的所有插件了,包括默认的插件

【讨论】:

以上是关于如何从 java 代码中获取所有插件,包括默认插件?马文的主要内容,如果未能解决你的问题,请参考以下文章

如何从 grails 默认 gsp 编码中排除插件?

如何从crm中的插件注册中恢复我的插件数据?

我们如何使用 Intl Tel Input 插件从给定号码获取国家代码?

从 Google Play 服务、Unity 插件获取所有回合制比赛信息

如何从 JBoss Drools Eclipse 插件获取审计窗口的日志文件?

提交表单后如何从(intl-tel-input)插件获取国家代码?