从命令行构buildEclipse Java项目

有没有办法从命令行编译基于Eclipse的Java项目?

我试图自动化我的构build(使用FinalBuilder而不是ant),我既不是Java也不是Eclipse专家。 我大概可以弄清楚如何用直接的java命令行选项来完成这个任务,但是Eclipse项目感觉像是浪费了大量的工作。

如果没有办法通过命令行来编译Eclipse项目,有没有办法在Eclipse中生成所需的java命令行? 还是有一些文件可以find它在幕后做的编译步骤?


伙计们,我正在寻找一个包括ant的答案。 让我重新重复原来的问题…….有没有办法从命令行构build一个Eclipse项目?

我不认为这是一个不合理的问题,因为我可以为visual studio做这样的事情:

devenv.exe /build "Debug|Any CPU" "C:\Projects\MyProject\source\MyProject.sln" 

你可以从命令行通过工作区build立一个eclipse项目:

 eclipsec.exe -noSplash -data "D:\Source\MyProject\workspace" -application org.eclipse.jdt.apt.core.aptBuild 

它使用jdt apt插件自动构build工作区。 这也被称为“无头build造”。 该死的很难弄清楚。 如果你不使用win32 exe,你可以试试这个:

 java -cp startup.jar -noSplash -data "D:\Source\MyProject\workspace" -application org.eclipse.jdt.apt.core.aptBuild 

更新

几年前,eclipse用“春分发射器”取代了startup.jar

https://wiki.eclipse.org/Equinox_Launcher

在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 

-data参数指定您的工作区的位置。

春分发射器的版本号将取决于你有什么版本的蚀。

为了完成André的回答,ant解决scheme可以像Emacs,JDEE,Ant和Eclipse Java Compiler中描述的一样 ,如下所示:

  <javac srcdir="${src}" destdir="${build.dir}/classes"> <compilerarg compiler="org.eclipse.jdt.core.JDTCompilerAdapter" line="-warn:+unused -Xemacs"/> <classpath refid="compile.classpath" /> </javac> 

compilerarg元素还允许您将其他命令行parameter passing给eclipse编译器。

你可以在这里find一个完整的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 

但是所有涉及ant的,这不是Keith所追求的。

对于批处理编译,请参考编译Java代码 ,特别是“ 使用批处理编译器

批处理编译器类位于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(Compiler API)的支持和对jsr269(Annotation processing)的支持。 为了使用注释处理支持,需要1.6 VM。

从命令行运行批处理编译器会给

 java -jar org.eclipse.jdt.core_3.4.0<qualifier>.jar -classpath rt.jar A.java 

要么:

 java -jar ecj.jar -classpath rt.jar A.java 

所有的java编译选项都在这一节中详细介绍。

与Visual Studio命令行编译function的不同之处在于, Eclipse似乎没有直接在命令行参数中读取它的.project和.classpath 。 您必须报告包含在.project和.classpath中的各种命令行选项中的所有信息才能获得相同的编译结果。

那么,简短的回答是:“是的,Eclipse有点类似”。 ;)

在27年之后,我也不习惯在IDE中开发。 我尝试了这些build议(上面) – 可能只是没有按照一切正确的 – 所以我做了一个networkingsearch,发现什么为我工作的“ http://incise.org/android-development-on-the- command-line.html '。

答案似乎是上述所有答案的组合(请告诉我,如果我错了,接受我的道歉,如果是的话)。

如上所述,eclipse / adt不会创build必要的ant文件。 为了在没有Eclipse IDE的情况下进行编译(并且不创buildant脚本):

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)接下来我编译这个项目。 我对请求不使用“ant”有点困惑。 希望 – 请求者意味着他不想写任何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)要将apk安装到设备上,我必须再次使用ant:

  ant target install ** my command line looked like ant debug install 

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)附注:从设备上查看日志使用:

  adb logcat 

3B)第二方面说明:上面提到的链接还包括从命令构build整个项目的说明。

希望这将有助于解决这个问题。 我知道我很高兴在这里find关于这个话题的任何信息。

正常的应用程序反过来工作:你创build基于Maven或ant的构build,然后使用集成为您的IDE的select,让你独立于它,尤其是。 当您尝试让新团队成员加速或使用连续集成服务器进行自动化构build时,这一点非常重要。 我build议使用maven,让它为你做繁重的工作。 创build一个pom文件,并通过mvn eclipse生成eclipse项目:eclipse。 HTH

这个问题包含了一些有关无头版本的有用链接,但是他们主要是针对构build插件的。 我不确定有多less可以应用到纯Java项目。

只是想增加我的两美分。 我尝试了@Kieveli针对非win32(下面重复)的build议,但是它对我没有帮助(在Eclipse上使用CentOS:Luna):

 java -cp startup.jar -noSplash -data "D:\Source\MyProject\workspace" -application org.eclipse.jdt.apt.core.aptBuild 

在我使用Eclipse(Luna)CentOS上的特殊设置,这工作:

 $ECLIPSE_HOME/eclipse -nosplash -application org.eclipse.jdt.apt.core.aptBuild startup.jar -data ~/workspace 

输出应该是这样的:

 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 

不太清楚为什么它显然做了两次,但似乎工作。

嗨除了VonC评论。 我正在使用ecj编译器来编译我的项目。 它正在抛出一些没有find的类。 但是这个项目用javac编译器很好的build立了。

所以只是我把类添加到类path(我们必须作为parameter passing),现在它的工作正常… 🙂

Kulbir Singh

简短的回答。 不。Eclipse没有像Visual Studio一样的命令行开关来构build项目。

日食通常做的只是build立java文件。 鉴于您应该能够从.classpath文件和.project文件中所需的源查看类path,并对其执行javac。 这应该很容易。