Maven插件组件注入零



我正在尝试编写自己的自定义Maven插件,该插件扩展了AbstrackDependendendencyMojo

问题是,所有atrackipdepentendencymojo组件均在我的插件的代码执行中为null。

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

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

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

插件被称为:

            <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注释来提供所有插件详细信息:

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

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

最新更新