如何使用Maven编译单个文件? [英] How to compile a single file with maven?
问题描述
我用mvn archetype:create ...
设置了一个可运行的Maven项目
我可以用mvn compile
编译所有源代码,但是我希望能够一次也编译一个文件.
I set up a working maven project with mvn archetype:create ...
I can compile all the sources with mvn compile
but I want to be able to compile also a single file at time.
有一种方法可以让Maven编译一个任意的.java文件吗?
There is a way to ask maven to compile an arbitrary single .java file?
谢谢
更新1:
我将在命令行上指定单个文件.我将在命令启动时选择文件.我不会修改pom.xml.
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.
更新2:
为什么我问这个?因为我会在Vim编辑器中使用autotest-java
.
Why I asked this? Because I would use autotest-java
with the Vim editor.
autotest-java
是ZenTest(这是一个Ruby应用程序,会在每次保存Ruby文件时自动启动单元测试)的黑客/扩展程序.
autotest-java
is an hack/extension of ZenTest (which is a Ruby application that automatically launch unit tests on every save of a Ruby file).
autotest-java
需要一个maven项目结构,并且要求您的编辑器/IDE执行保存时编译".实际上,它不会识别何时修改.java文件,而是会监视.class文件进行修改.
autotest-java
requires 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与Netbeans一起使用(它支持保存时编译"),但是我将其与Vim一起使用.
I succesfully used autotest-java with Netbeans (which support "compile on save") but I would use it with Vim.
推荐答案
据我所知,这在命令行上是不可能的.值得注意的是(可选)参数 excludes
和 includes
不推荐使用您可以在pom.xml
中定义的用于配置Compiler Plugin的a>,如下所示:
To my knowledge, this is not possible on the the command line. And it is worth noting that the (optional) parameters excludes
and includes
that you can define to configure the Compiler Plugin in the pom.xml
are deprecated as shown below:
$ 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.
但这导致一个问题.由于Maven使用增量编译,即仅编译自上次构建以来磁盘上已更改的类,因此为什么只需要编译一个类?您的用例是什么?
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编译单个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!