繁体   English   中英

maven插件组件注入null

[英]maven plugin components injection null

我正在尝试编写自己的自定义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
  }
}

在调试时,project为null,factory为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 ,也可能是原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM