Generating classpath file with Maven

守給你的承諾、 提交于 2020-01-24 10:59:09

问题


I'd like to generate a classpath file from pom.xml dependencies. I need it so during tests I have the classpath of all dependencies (that are later packaged into a bundle)

maven-dependency-plugin does not suit me for two reasons:

  • it generates paths to files in the repository, so to use other modules they first need to run install phase for them (I'd like to have paths like /some/root/othermodule/target/classes)
  • it doesn't include the artifact's own path (target/classes), which means I need to add it later in code, which is awkward

So I'm looking for another plugin (or how to properly run maven-dependency-plugin)


回答1:


I ended up using 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>

The code is a bit complex since I wanted paths to target/classes when possible. If this is not required, one can do :

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


来源:https://stackoverflow.com/questions/3952846/generating-classpath-file-with-maven

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!