如何从maven插件添加编译类路径依赖项?

时间:2015-12-14 09:27:01

标签: maven maven-3 maven-plugin

我有非maven java项目。我想创建第二个项目,这次使用maven性质来保存主应用程序的所有集成和单元测试。要从CLI运行测试,我需要将主项目中的依赖项包含在" maven style"中的测试项目中。由于没有开箱即用的解决方案,我决定为此创建新的Maven插件:

因此,我的插件将分析Eclipse的.islass文件,并将正确的URL添加到编译时库的集合中。如何从maven插件中注入这样的依赖项?

我在互联网上找到了这样的东西:

    @Parameter(defaultValue = "${project}", required = true, readonly = true)
    private MavenProject project;

project具有很好看的方法,例如getCompileClasspathElements()。但是由于某些原因我无法使用它,因为依赖程序未解决:

  

描述资源路径位置类型   由于构建路径不完整,因此未构建项目。找不到org.apache.maven.artifact.DependencyResolutionRequiredException的类文件。修复构建路径,然后尝试构建此项目include-eclipse-project-maven-plugin Unknown Java Problem

我在哪里可以找到这门课程?它不应该作为依赖包含在maven-project-plugin中吗?在maven生命周期运行时修改类路径的正确方法是什么?

这是插件的POM:

<dependencies>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-plugin-api</artifactId>
        <version>3.3.9</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.plugin-tools</groupId>
        <artifactId>maven-plugin-annotations</artifactId>
        <version>3.2</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.codehaus.plexus</groupId>
        <artifactId>plexus-utils</artifactId>
        <version>3.0.8</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version>1.6.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-project</artifactId>
        <version>2.2.1</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-plugin-plugin</artifactId>
            <version>3.4</version>
            <configuration>
                <goalPrefix>include-eclipse-project</goalPrefix>
                <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>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>

    </plugins>
</build>
<profiles>
    <profile>
        <id>run-its</id>
        <build>

            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-invoker-plugin</artifactId>
                    <version>1.7</version>
                    <configuration>
                        <debug>true</debug>
                        <cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>
                        <pomIncludes>
                            <pomInclude>*/pom.xml</pomInclude>
                        </pomIncludes>
                        <postBuildHookScript>verify</postBuildHookScript>
                        <localRepositoryPath>${project.build.directory}/local-repo</localRepositoryPath>
                        <settingsFile>src/it/settings.xml</settingsFile>
                        <goals>
                            <goal>clean</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </configuration>
                    <executions>
                        <execution>
                            <id>integration-test</id>
                            <goals>
                                <goal>install</goal>
                                <goal>integration-test</goal>
                                <goal>verify</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>

        </build>
    </profile>
</profiles>

修改 在POM中更改依赖关系之后,插件是可编译的,但是在编译阶段会将注入的artifcats注入到MavenProject对象中。

我正在通过

添加deps

public void setDependencyArtifacts(Set<Artifact> dependencyArtifacts)

并通过

列出
public Set<Artifact> getArtifacts()

0 个答案:

没有答案