java - maven插件组件注入(inject)null

标签 java maven plugins components pom.xml

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

问题是,在我的插件的代码执行中,所有 AbstractDependencyMojo 组件均为 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
  }
}

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

显然,由于某些原因,组件注入(inject)根本不起作用,我不明白为什么......

插件的调用方式如下:

            <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,这也可能是原因。

关于java - maven插件组件注入(inject)null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37919180/

相关文章:

java - org.apache.catalina.LifecycleException : Failed to start component [StandardEngine[Catalina]. StandardHost[本地主机].StandardContext[/mmasgis]]

java - Maven 将 native 添加到库路径

eclipse - 如何以编程方式从完整类名中查找 Eclipse 插件中的 .java 文件?

javascript - 浏览器插件检测

java - getHibernateTemplate.save() - 如何获得受影响的行/新的自动增量

java - 如何相对于彼此对两个数组进行排序。

java - Property [project] 标有相互矛盾的注释

spring - 如何在 Kotlin 中使用 QueryDSL 并生成文件

java - Apache Maven : cannot find symbol and invalid target release error (NetBeans support for Mule Plugin)

Java "int i = byte1 | 0x0200"与 "int i = byte1"?