从命令行构建 Eclipse Java 项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/206473/
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
Build Eclipse Java Project from Command Line
提问by Keith G
Is there a way to compile an Eclipse-based Java project from the command line?
有没有办法从命令行编译基于 Eclipse 的 Java 项目?
I'm trying to automate my build (using FinalBuilder not ant), and I'm neither a Java nor Eclipse expert. I can probably figure out how to do this with straight java command line options, but then the Eclipse project feels like a lot of wasted effort.
我正在尝试自动化我的构建(使用 FinalBuilder 而不是 ant),我既不是 Java 也不是 Eclipse 专家。我大概可以弄清楚如何使用直接的 java 命令行选项来做到这一点,但是 Eclipse 项目感觉就像是浪费了很多精力。
In the event that there is no way to compile an Eclipse project via the command line, is there a way to generate the required java command line from within Eclipse? Or are there some files I can poke around to find the compile steps it is doing behind the scenes?
如果无法通过命令行编译 Eclipse 项目,有没有办法从 Eclipse 中生成所需的 java 命令行?或者是否有一些文件我可以四处查看以找到它在幕后所做的编译步骤?
Guys, I'm looking for an answer that does NOTinclude ant. Let me re-iterate the original question ....... Is there a way to build an Eclipse project from the command line?
伙计们,我正在寻找不包括蚂蚁的答案。让我再次重复最初的问题....... 有没有办法从命令行构建 Eclipse 项目?
I don't think this is an unreasonable question given that I can do something like this for visual studio:
我不认为这是一个不合理的问题,因为我可以为 Visual Studio 做这样的事情:
devenv.exe /build "Debug|Any CPU" "C:\Projects\MyProject\source\MyProject.sln"
采纳答案by Kieveli
You can build an eclipse project via a workspace from the command line:
您可以从命令行通过工作区构建 Eclipse 项目:
eclipsec.exe -noSplash -data "D:\Source\MyProject\workspace" -application org.eclipse.jdt.apt.core.aptBuild
It uses the jdt apt
plugin to build your workspace automatically. This is also known as a 'Headless Build'. Damn hard to figure out. If you're not using a win32 exe, you can try this:
它使用jdt apt
插件自动构建您的工作区。这也称为“无头构建”。该死的很难弄清楚。如果你没有使用 win32 exe,你可以试试这个:
java -cp startup.jar -noSplash -data "D:\Source\MyProject\workspace" -application org.eclipse.jdt.apt.core.aptBuild
Update
更新
Several years ago eclipse replaced startup.jar
with the "equinox launcher"
几年前eclipse被startup.jar
“equinox launcher”取代
https://wiki.eclipse.org/Equinox_Launcher
https://wiki.eclipse.org/Equinox_Launcher
On Eclipse Mars (MacOX):
在 Eclipse Mars (MacOX) 上:
java -jar /Applications/Eclipse.app/Contents/Eclipse/plugins/org.eclipse.equinox.launcher_1.3.100.v20150511-1540.jar -noSplash -data "workspace" -application org.eclipse.jdt.apt.core.aptBuild
The -data
parameter specifies the location of your workspace.
该-data
参数指定工作区的位置。
The version number for the equinox launcher will depend on what version of eclipse you have.
Equinox 启动器的版本号将取决于您拥有的 eclipse 版本。
回答by André
The normal apporoach works the other way around: You create your build based upon mavenor antand then use integrations for your IDE of choice so that you are independent from it, which is esp. important when you try to bring new team members up to speed or use a contious integration server for automated builds. I recommend to use maven and let it do the heavy lifting for you. Create a pom file and generate the eclipse project via mvn eclipse:eclipse. HTH
正常的方法相反:您基于maven或ant创建构建,然后为您选择的 IDE 使用集成,以便您独立于它,尤其是。当您尝试让新团队成员跟上进度或使用连续集成服务器进行自动构建时,这一点很重要。我建议使用 maven 并让它为您完成繁重的工作。创建一个pom文件并通过mvn eclipse:eclipse生成eclipse项目。HTH
回答by JesperE
This questioncontains some useful links on headless builds, but they are mostly geared towards building plugins. I'm not sure how much of it can be applied to pure Java projects.
这个问题包含一些关于无头构建的有用链接,但它们主要用于构建插件。我不确定其中有多少可以应用于纯 Java 项目。
回答by VonC
To complete André's answer, an ant solution could be like the one described in Emacs, JDEE, Ant, and the Eclipse Java Compiler, as in:
为了完成安德烈的回答,蚂蚁解决方案可能类似于Emacs、JDEE、Ant 和 Eclipse Java 编译器中描述的解决方案,如下所示:
<javac
srcdir="${src}"
destdir="${build.dir}/classes">
<compilerarg
compiler="org.eclipse.jdt.core.JDTCompilerAdapter"
line="-warn:+unused -Xemacs"/>
<classpath refid="compile.classpath" />
</javac>
The compilerarg element also allows you to pass in additional command line args to the eclipse compiler.
compilerarg 元素还允许您将额外的命令行参数传递给 eclipse 编译器。
You can find a full ant script example herewhich would be invoked in a command linewith:
您可以在此处找到完整的 ant 脚本示例,该示例将在命令行中调用:
java -cp C:/eclipse-SDK-3.4-win32/eclipse/plugins/org.eclipse.equinox.launcher_1.0.100.v20080509-1800.jar org.eclipse.core.launcher.Main -data "C:\Documents and Settings\Administrator\workspace" -application org.eclipse.ant.core.antRunner -buildfile build.xml -verbose
BUT all that involves ant, which is not what Keith is after.
但所有涉及蚂蚁的事情,这不是基思所追求的。
For a batch compilation, please refer to Compiling Java code, especially the section "Using the batch compiler"
批量编译请参考编译Java代码,特别是“使用批量编译器”一节
The batch compiler class is located in the JDT Core plug-in. The name of the class is org.eclipse.jdt.compiler.batch.BatchCompiler. It is packaged into plugins/org.eclipse.jdt.core_3.4.0..jar. Since 3.2, it is also available as a separate download. The name of the file is ecj.jar.
Since 3.3, this jar also contains the support for jsr199 (Compiler API) and the support for jsr269 (Annotation processing). In order to use the annotations processing support, a 1.6 VM is required.
批处理编译器类位于 JDT Core 插件中。类的名称是 org.eclipse.jdt.compiler.batch.BatchCompiler。它被打包到 plugins/org.eclipse.jdt.core_3.4.0..jar 中。从 3.2 开始,它也可以单独下载。该文件的名称是 ecj.jar。
从 3.3 开始,这个 jar 也包含了对 jsr199(编译器 API)的支持和对 jsr269(注解处理)的支持。为了使用注释处理支持,需要 1.6 VM。
Running the batch compiler From the command line would give
从命令行运行批处理编译器会给出
java -jar org.eclipse.jdt.core_3.4.0<qualifier>.jar -classpath rt.jar A.java
or:
或者:
java -jar ecj.jar -classpath rt.jar A.java
All java compilation options are detailed in that section as well.
该部分还详细介绍了所有 java 编译选项。
The difference with the Visual Studio command line compilation feature is that Eclipse does not seem to directly read its .project and .classpath in a command-line argument. You have to report all information contained in the .project and .classpath in various command-line options in order to achieve the very same compilation result.
与 Visual Studio 命令行编译功能的不同之处在于Eclipse 似乎不会在命令行参数中直接读取其 .project 和 .classpath。您必须在各种命令行选项中报告 .project 和 .classpath 中包含的所有信息,以获得完全相同的编译结果。
So, then short answer is: "yes, Eclipse kind of does." ;)
所以,简短的回答是:“是的,Eclipse 确实如此。” ;)
回答by pdeva
Short answer. No. Eclipse does not have a command line switch like Visual Studio to build a project.
简短的回答。否。Eclipse 没有像 Visual Studio 这样的命令行开关来构建项目。
回答by pdeva
Hi Just addition to VonC comments. I am using ecj compiler to compile my project. it was throwing expcetion that some of the classes are not found. But the project was bulding fine with javac compiler.
您好 只是对 VonC 评论的补充。我正在使用 ecj 编译器来编译我的项目。令人难以置信的是,找不到某些类。但是该项目使用 javac 编译器构建得很好。
So just I added the classes into the classpath(which we have to pass as argument) and now its working fine... :)
所以我只是将类添加到类路径中(我们必须将其作为参数传递),现在它工作正常...... :)
Kulbir Singh
库比尔·辛格
回答by Charles Thomas
After 27 years, I too, am uncomfortable developing in an IDE. I tried these suggestions (above) - and probably just didn't follow everything right -- so I did a web-search and found what worked for me at 'http://incise.org/android-development-on-the-command-line.html'.
27 年后,我也对在 IDE 中开发感到不舒服。我尝试了这些建议(以上) - 可能只是没有按照正确的方式进行 - 所以我进行了网络搜索并在' http://incise.org/android-development-on-the-上找到了对我有用的方法命令行.html'。
The answer seemed to be a combination of all the answers above (please tell me if I'm wrong and accept my apologies if so).
答案似乎是上述所有答案的组合(如果我错了,请告诉我,如果是,请接受我的道歉)。
As mentioned above, eclipse/adt does not create the necessary ant files. In order to compile without eclipse IDE (and without creating ant scripts):
如上所述,eclipse/adt 不会创建必要的 ant 文件。为了在没有 Eclipse IDE 的情况下进行编译(并且没有创建 ant 脚本):
1) Generate build.xml in your top level directory:
1) 在顶级目录中生成 build.xml:
android list targets (to get target id used below)
android update project --target target_id --name project_name --path top_level_directory
** my sample project had a target_id of 1 and a project name of 't1', and
I am building from the top level directory of project
my command line looks like android update project --target 1 --name t1 --path `pwd`
2) Next I compile the project. I was a little confused by the request to not use 'ant'. Hopefully -- requester meant that he didn't want to write any ant scripts. I say this because the next step is to compile the application using ant
2)接下来我编译项目。我对不使用“ant”的请求感到有些困惑。希望——请求者的意思是他不想编写任何蚂蚁脚本。我这样说是因为下一步是使用 ant 编译应用程序
ant target
this confused me a little bit, because i thought they were talking about the
android device, but they're not. It's the mode (debug/release)
my command line looks like ant debug
3) To install the apk onto the device I had to use ant again:
3)要将apk安装到设备上,我不得不再次使用ant:
ant target install
** my command line looked like ant debug install
4) To run the project on my android phone I use adb.
4) 要在我的 android 手机上运行该项目,我使用 adb。
adb shell 'am start -n your.project.name/.activity'
** Again there was some confusion as to what exactly I had to use for project
My command line looked like adb shell 'am start -n com.example.t1/.MainActivity'
I also found that if you type 'adb shell' you get put to a cli shell interface
where you can do just about anything from there.
3A) A side note: To view the log from device use:
3A) 旁注:从设备使用中查看日志:
adb logcat
3B) A second side note: The link mentioned above also includes instructions for building the entire project from the command.
3B) 第二个附注:上面提到的链接还包括从命令构建整个项目的说明。
Hopefully, this will help with the question. I know I was really happy to find anythingabout this topic here.
希望这将有助于解决这个问题。我知道我很高兴在这里找到有关此主题的任何信息。
回答by jkwinn26
Just wanted to add my two cents to this. I tried doing as @Kieveli suggested for non win32 (repeated below) but it didn't work for me (on CentOS with Eclipse: Luna):
只是想加上我的两分钱。我尝试按照@Kieveli 对非 win32 的建议进行操作(在下面重复),但对我不起作用(在 CentOS 上使用 Eclipse:Luna):
java -cp startup.jar -noSplash -data "D:\Source\MyProject\workspace" -application org.eclipse.jdt.apt.core.aptBuild
On my particular setup on CentOS using Eclipse (Luna) this worked:
在我使用 Eclipse (Luna) 在 CentOS 上的特定设置中,这有效:
$ECLIPSE_HOME/eclipse -nosplash -application org.eclipse.jdt.apt.core.aptBuild startup.jar -data ~/workspace
The output should look something like this:
输出应如下所示:
Building workspace
Building '/RemoteSystemsTempFiles'
Building '/test'
Invoking 'Java Builder' on '/test'.
Cleaning output folder for test
Build done
Building workspace
Building '/RemoteSystemsTempFiles'
Building '/test'
Invoking 'Java Builder' on '/test'.
Preparing to build test
Cleaning output folder for test
Copying resources to the output folder
Analyzing sources
Compiling test/src/com/company/test/tool
Build done
Not quite sure why it apparently did it twice, but it seems to work.
不太清楚为什么它显然做了两次,但它似乎有效。