Maven:将文件夹或 jar 文件添加到当前类路径中
我正在使用 maven-compile 插件来编译类。现在我想将一个 jar 文件添加到当前的类路径中。该文件保留在另一个位置(假设 c:/jars/abc.jar 。我更喜欢将此文件留在这里)。我怎样才能做到这一点?
如果我在参数中使用类路径:
<configuration>
<compilerArguments>
<classpath>c:/jars/abc.jar</classpath>
</compilerArguments>
</configuration>
它将不起作用,因为它将覆盖当前的类路径(包括所有依赖项)
I am using maven-compile plugin to compile classes. Now I would like to add one jar file into the current classpath. That file stays in another location (let's say c:/jars/abc.jar . I prefer to leave this file here). How can I do that?
If I use classpath in the argument:
<configuration>
<compilerArguments>
<classpath>c:/jars/abc.jar</classpath>
</compilerArguments>
</configuration>
it will not work because it will override the current classpath (that includes all the dependencies)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
以前可能有人问过这个问题。请参阅 我可以将 jar 添加到maven 2 构建类路径而不安装它们?
简而言之:将您的 jar 包含为系统范围的依赖项。这需要指定 jar 的绝对路径。
另请参见 http://maven.apache.org/guides/简介/依赖机制介绍.html
This might have been asked before. See Can I add jars to maven 2 build classpath without installing them?
In a nutshell: include your jar as dependency with system scope. This requires specifying the absolute path to the jar.
See also http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
编译器插件的类路径设置有两个参数。像这样改变它,它对我有用:
我使用 gmavenplus-plugin 来读取路径并创建属性“cp”:
The classpath setting of the compiler plugin are two args. Changed it like this and it worked for me:
I used the gmavenplus-plugin to read the path and create the property 'cp':
来自 文档 和 example 目前尚不清楚是否允许类路径操作。
但请参阅Java 文档(也https://www.cis.upenn.edu /~bcpierce/courses/629/jdkdocs/tooldocs/solaris/javac.html)
也许可以获取当前的类路径并扩展它,
参见在maven中,如何输出正在使用的类路径?
阅读文件(将文件读入 Maven 属性)
,最后
From docs and example it is not clear that classpath manipulation is not allowed.
But see Java docs (also https://www.cis.upenn.edu/~bcpierce/courses/629/jdkdocs/tooldocs/solaris/javac.html)
Maybe it is possible to get current classpath and extend it,
see in maven, how output the classpath being used?
Read file (Read a file into a Maven property)
and finally
我们混合了此处找到的两个答案来解决类似的问题。我们的项目仅在编译阶段需要 JAR,但使用 system 范围添加本地依赖项,这是没有用的,因为 Maven 拒绝工件发布,并出现与缺少依赖项相关的错误。
使用的片段如下:
Maven 能够编译并成功部署工件。
如果有人感兴趣,完整的 POM 可以在 GitHub 项目 NuReflector 下找到,defaultPOM.template< /em> 在 src/NuReflector 下。
We mixed two of the answers found here to solve a similar problem. Our project needs a JAR only in compile stage, but add a local dependency, using system scope, it is unuseful because Maven refuse the artifact publication with an error related to a missing dependency.
The snippets used are the following:
Maven is able to compile and successfully deploy the artifacts.
If anyone is interested the full POM is available in GitHub under project NuReflector, defaultPOM.template under src/NuReflector.
使用 antrun 插件的解决方案(将 your_jar_or_classes_folder 添加到类路径):
Solution with antrun plugin (adds your_jar_or_classes_folder to classpath):