java 如何用maven编译单个文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1969150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compile a single file with maven?
提问by Andrea Francia
I set up a working maven project with mvn archetype:create ...I can compile all the sources with mvn compilebut I want to be able to compile also a single file at time.
我建立了一个工作 Maven 项目,mvn archetype:create ...我可以编译所有源代码,mvn compile但我希望能够同时编译一个文件。
There is a way to ask maven to compile an arbitrary single .java file?
有没有办法让 maven 编译一个任意的单个 .java 文件?
Thanks
谢谢
Updates 1:
更新一:
I would specify the single file on the command line. I would choose the file at the moment of the command launch. I wouldn't modify the pom.xml.
我会在命令行上指定单个文件。我会在命令启动时选择文件。我不会修改 pom.xml。
Update 2:
更新 2:
Why I asked this? Because I would use autotest-javawith the Vim editor.
我为什么问这个?因为我会使用autotest-javaVim 编辑器。
autotest-javais an hack/extension of ZenTest (which is a Ruby application that automatically launch unit tests on every save of a Ruby file).
autotest-java是 ZenTest(这是一个 Ruby 应用程序,可在每次保存 Ruby 文件时自动启动单元测试)的 hack/扩展。
autotest-javarequires a maven project structure and requires that your editor/IDE perform "compile on save". In fact it doesn't recognize when a .java file is modified but instead it monitors for .class files for modifications.
autotest-java需要 Maven 项目结构,并要求您的编辑器/IDE 执行“保存时编译”。事实上,它不识别 .java 文件何时被修改,而是监视 .class 文件的修改。
I succesfully used autotest-java with Netbeans (which support "compile on save") but I would use it with Vim.
我成功地将 autotest-java 与 Netbeans(支持“保存时编译”)一起使用,但我会将它与 Vim 一起使用。
回答by Pascal Thivent
To my knowledge, this is not possible on the the command line. And it is worth noting that the (optional) parameters excludesand includesthat you can define to configure the Compiler Plugin in the pom.xmlare deprecated as shown below:
据我所知,这在命令行上是不可能的。值得注意的是,(可选)参数excludes以及includes您可以定义以配置 Compiler Plugin 中的参数pom.xml已被弃用,如下所示:
$ mvn help:describe -Dcmd=compiler:compile -Ddetail
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'help'.
[INFO] ------------------------------------------------------------------------
[INFO] Building sandbox
[INFO] task-segment: [help:describe] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] [help:describe {execution: default-cli}]
[INFO] 'compiler:compile' is a plugin goal (aka mojo).
Mojo: 'compiler:compile'
compiler:compile
Description: Compiles application sources
Deprecated. No reason given
Implementation: org.apache.maven.plugin.CompilerMojo
Language: java
Bound to phase: compile
Available parameters:
...
excludes
A list of exclusion filters for the compiler.
Deprecated. No reason given
...
includes
A list of inclusion filters for the compiler.
Deprecated. No reason given
...
So there is actually no real way to do what you want on the long term.
所以实际上没有真正的方法可以长期做你想做的事。
But this leads to one question. Since Maven uses incremental compilation i.e. it compiles only classes that changed on disk since the last build, why would you need to compile only one class? What is your use case?
但这引出了一个问题。由于 Maven 使用增量编译,即它只编译自上次构建以来在磁盘上更改的类,为什么您只需要编译一个类?你的用例是什么?
回答by rodrigoap
You will have to configure the compiler pluginto excludethe other files or to includejust that one.
您必须配置编译器插件以排除其他文件或仅包含该文件。
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!-- put your configurations here -->
</configuration>
</plugin>
</plugins>
</build>
...
</project>

