【问题标题】:maven plugin components injection nullmaven插件组件注入null
【发布时间】:2016-10-21 12:17:06
【问题描述】:

我正在尝试编写自己的自定义 maven 插件它扩展了 AbstractDependencyMojo

问题是所有 AbstractDependencyMojo 组件在我的插件的代码执行时都是空的。

@goal generate-dependencies    
@phase install
@requiresProject false

见下面的代码,重写方法execute()

  public void execute() throws MojoExecutionException {
List<Dependency> dependencies = project.getModel().getDependencies();

for (Dependency dependency : dependencies) {
  try {

    Artifact a = factory.createArtifact(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(), dependency.getScope(), dependency.getType());
    resolver.resolve(a, remoteRepos, getLocal());

  }
  catch (ArtifactResolutionException e) {
    throw new MojoExecutionException("Implicit artifact resolution failed", e);
  }
  catch (ArtifactNotFoundException e) {
    // Do nothing
  }
}

调试时,项目为空,工厂为空,一切都是..为空。

显然,由于某些原因,组件注入根本不起作用,我不知道为什么....

插件是这样调用的:

            <plugin>
            <groupId>com.me.framework</groupId>
            <artifactId>plugin-dependency-plugin</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <executions>
                <execution>
                    <id>plugin-dependencies</id>
                    <phase>install</phase>
                    <goals>
                        <goal>generate-dependencies</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

你有什么想法吗?谢谢

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.me.framework</groupId>
<artifactId>plugin-dependency-plugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>maven-plugin</packaging>

<dependencies>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-model-builder</artifactId>
        <version>3.0.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-plugin-api</artifactId>
        <version>2.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-project</artifactId>
        <version>2.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.plugin-tools</groupId>
        <artifactId>maven-plugin-annotations</artifactId>
        <version>3.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <type>maven-plugin</type>
        <version>2.5.1</version>
    </dependency>
</dependencies>

<build>
    <pluginManagement>
        <plugins>
            <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>org.apache.maven.plugins</groupId>
                                    <artifactId>maven-plugin-plugin</artifactId>
                                    <versionRange>[3.2,)</versionRange>
                                    <goals>
                                        <goal>descriptor</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore></ignore>
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

调用插件 pom

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>TheArtifact</artifactId>
<dependencies>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-clean-plugin</artifactId>
            <executions>
                <execution>
                    <id>clean-generated-sources</id>
                    <phase>clean</phase>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <!-- Package project as artifact (for apidoc) -->
                <execution>
                    <id>package-project</id>
                    <phase>prepare-package</phase>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>com.me.framework</groupId>
            <artifactId>plugin-dependency-plugin</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <executions>
                <execution>
                    <id>plugin-dependencies</id>
                    <phase>install</phase>
                    <goals>
                        <goal>generate-dependencies</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <!-- Install project resources as artifact (for apidoc) -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>install-project</id>
                    <phase>prepare-package</phase>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

【问题讨论】:

  • 你能发布你的整个 MOJO 而不是简短的 sn-ps 吗?
  • 您发布了您的 POM,但没有发布完整的 Java 类。你也可以发一下吗?
  • 你只能写一个从AbstractMojo扩展的插件。 AbstractDependencyMojo 来自其他插件...因此您可能需要分叉此插件并增强实现..

标签: java maven plugins components pom.xml


【解决方案1】:

首先,基于文档的标记已经过时,最好使用@Mojo 注解来提供所有插件详细信息:

@Mojo(
    name = "generate-dependencies",
    defaultPhase = LifecyclePhase.INSTALL,
    requiresDependencyResolution = ResolutionScope.COMPILE
    // ...
)

其次,我会检查 @requiresProject 是否设置为 false 并尝试设置为 true,这也可能是原因。

【讨论】:

  • 这是否适用于 Maven 2,就像他们显然正在使用的那样?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-03
  • 1970-01-01
  • 1970-01-01
  • 2021-11-19
  • 1970-01-01
  • 2016-06-12
相关资源
最近更新 更多