代码之家  ›  专栏  ›  技术社区  ›  IttayD

用maven生成类路径文件

  •  3
  • IttayD  · 技术社区  · 15 年前

    我想从pom.xml依赖项生成一个类路径文件。我需要它,所以在测试期间,我拥有所有依赖项的类路径(这些依赖项稍后打包成一个包)

    maven-dependency-plugin 不适合我有两个原因:

    • 它生成指向存储库中文件的路径,以便使用它们首先需要运行的其他模块。 install 阶段为他们(我想有这样的路径 /some/root/othermodule/target/classes )
    • 它不包括工件自己的路径( target/classes ,这意味着我需要稍后在代码中添加它,这很尴尬

    所以我正在寻找另一个插件(或者如何正确运行 Maven依赖插件 )

    1 回复  |  直到 15 年前
        1
  •  2
  •   IttayD    15 年前

    我最终使用了gmaven:

            <plugin>
                <groupId>org.codehaus.groovy.maven</groupId>
                <artifactId>gmaven-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <source>
                                def all = project.runtimeArtifacts.collect{
                                    def aid = "${it.groupId}:${it.artifactId}:${it.version}"
                                    def p = project.projectReferences[aid]
                                    p?.build?.outputDirectory ?: it.file.path
                                } + project.build.outputDirectory
                                def file = new File(project.build.directory, ".classpath")
                                file.write(all.join(File.pathSeparator))
                            </source>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    

    代码有点复杂,因为我希望在可能的情况下找到指向目标/类的路径。如果不需要,可以这样做:

    file.write(project.runtimeClasspathElements.join(File.pathSeparator))