java - 如何使用 API 3.1.1 在 Maven 插件中使用 Aether (eclipse)?

标签 java maven-3 maven-plugin aether

我正在使用 API v3.1.1 开发一个新的 Maven 插件,因为我需要升级到 Maven 3.1.1,并且需要 Aether 方式来处理工件存储库,以及检索工件版本的完整列表等。我正在使用 Eclipse Aether (0.9.0.M4),不是 Sonatype Aether。

我已经通读了 http://wiki.eclipse.org/Aether并尝试了演示 http://git.eclipse.org/c/aether/aether-demo.git/tree/ , 但我无法理解为什么以下 within AbstractMojo 的子类不起作用。

两者都是 RepositorySystem repoSystem , RepositorySystemSession repoSession , List<RemoteRepository> projectRepos , 和 List<RemoteRepository> pluginReposnull .

我也尝试过使用 @Component注入(inject)具有相同结果的那些。

为了将这些对象注入(inject) mojo,我是否遗漏了什么?

import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.repository.RemoteRepository;
...

public MyMojo extends AbstractMojo
{ 

    /**
     * The entry point to Aether, i.e. the component doing all the work.
     * 
     * @component
     */
    private RepositorySystem repoSystem;

    /**
     * The current repository/network configuration of Maven.
     * 
     * @parameter default-value="${repositorySystemSession}"
     * @readonly
     */
    private RepositorySystemSession repoSession;

    /**
     * The project's remote repositories to use for the resolution of project dependencies.
     * 
     * @parameter default-value="${project.remoteProjectRepositories}"
     * @readonly
     */
    private List<RemoteRepository> projectRepos;

    /**
     * The project's remote repositories to use for the resolution of plugins and their dependencies.
     * 
     * @parameter default-value="${project.remotePluginRepositories}"
     * @readonly
     */
    private List<RemoteRepository> pluginRepos;

    // Your other mojo parameters and code here
    ...
}

最佳答案

最后,它对我有用,我认为它之前不起作用的原因是我的 pom.xml 中有太多的依赖关系,事情没有得到正确的解决。

这是我使用的完整 pom.xml,以便原始帖子中的代码正常工作。其余代码来自 aether-demo,原始帖子中也提供了链接

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

    <groupId>aether.example</groupId>
    <artifactId>my-aether-plugin</artifactId>
    <version>1.0.1-SNAPSHOT</version>
    <packaging>maven-plugin</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <mavenVersion>3.1.1</mavenVersion>
        <aetherVersion>0.9.0.M4</aetherVersion>
        <mavenPluginVersion>3.2</mavenPluginVersion>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-plugin-plugin</artifactId>
                <version>${mavenPluginVersion}</version>
                <configuration>
                    <goalPrefix>my-aether</goalPrefix>
                    <!-- see http://jira.codehaus.org/browse/MNG-5346 -->
                    <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
                </configuration>
                <executions>
                    <execution>
                        <id>mojo-descriptor</id>
                        <goals>
                            <goal>descriptor</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>help-goal</id>
                        <goals>
                            <goal>helpmojo</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

    <dependencies>  
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-plugin-api</artifactId>
            <version>${mavenVersion}</version>
            <scope>provided</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.maven</groupId>
                    <artifactId>maven-model</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.maven</groupId>
                    <artifactId>maven-artifact</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.eclipse.sisu</groupId>
                    <artifactId>org.eclipse.sisu.plexus</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
         <dependency>
             <groupId>org.eclipse.aether</groupId>
             <artifactId>aether-api</artifactId>
             <version>${aetherVersion}</version>
         </dependency>
         <dependency>
             <groupId>org.eclipse.aether</groupId>
             <artifactId>aether-util</artifactId>
             <version>${aetherVersion}</version>
         </dependency>

         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
             <version>4.11</version>
             <scope>test</scope>
         </dependency>
    </dependencies>
</project>

关于java - 如何使用 API 3.1.1 在 Maven 插件中使用 Aether (eclipse)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21612945/

相关文章:

java - Spring + Atmoting + Tomcat 线程泄漏

java - 尝试计算 Pi,但只得到 1

java - 只是我想在 Maven 项目构建失败时创建目标

java - 在远程weblogic 12c上使用maven插件进行部署

javascript - 没有 Node js 的 ES6 或 uglify minify maven 插件

java - 如何显示计算器应用程序最近5次的计算结果?

java - 为什么这个 String HashMap 不能通过 *char* 键得到正确的值

maven-3 - 从测试范围 Maven 中排除 servlet-api

maven - 无法将存储库添加到本地 settings.xml

dependencies - 如果我跳过正在运行的测试,我可以将 Maven 配置为忽略 "test"范围的依赖项吗?