【问题标题】:Generating classpath file with Maven使用 Maven 生成类路径文件
【发布时间】:2023-04-04 05:48:02
【问题描述】:

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

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

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

所以我正在寻找另一个插件(或如何正确运行maven-dependency-plugin

【问题讨论】:

  • 测试的类路径是自动生成的。那么具体问题在哪里呢?还是您在谈论一种集成测试而不是单元测试? (多模块构建?)
  • 在一个组件中,我正在为另一个组件创建一个 ClassLoader。在生产中,类路径是 lib/*,但在测试中我不想经过打包

标签: maven-2 plugins dependencies


【解决方案1】:

我最终使用了 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))

【讨论】:

    猜你喜欢
    • 2011-06-05
    • 2011-09-19
    • 2013-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多