问题

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

我正在尝试自动化我的构建(使用FinalBuilder而不是ant),而且我既不是Java也不是Eclipse专家。我可以用直接的java命令行选项找出如何做到这一点,但Eclipse项目感觉就像浪费了很多精力。

如果无法通过命令行编译Eclipse项目,是否有办法从Eclipse中生成所需的java命令行?或者是否有一些文件我可以找到它在幕后进行的编译步骤?

伙计们,我正在寻找一个不包含蚂蚁的答案。让我重新解释原始问题.......有没有办法从命令行构建Eclipse项目?

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

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

#1 热门回答(56 赞)

你可以从命令行通过工作区构建eclipse项目:

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

它使用jdt aptplugin自动构建工作区。这也被称为"无头建筑"。该死的很难搞清楚。如果你没有使用win32 exe,可以试试这个:

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

更新
几年前,eclipse用"equinox发射器"取代了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

The-dataparameter指定工作区的位置。

equinox启动器的版本号将取决于你拥有的日食版本。


#2 热门回答(21 赞)

为了完成André的答案,蚂蚁解决方案可能与Emacs, JDEE, Ant, and the 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元素还允许你将其他命令行参数传递给eclipse编译器。

你可以在命令行中找到afull ant script example here

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

但是所有这些都涉及蚂蚁,这不是基思所追求的。

有关批量编译,请参阅Compiling Java code,尤其是"使用批处理编译器"部分

批处理编译器类位于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。

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

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命令行编译功能的不同之处在于,Eclipse似乎没有在命令行参数中直接读取其.project和.classpath。你必须在各种命令行选项中报告.project和.classpath中包含的所有信息,以便实现完全相同的编译结果。

那么,简短的回答是:"是的,Eclipse就是这样。" ;)


#3 热门回答(7 赞)

经过27年,我也在IDE中开发很不舒服。我尝试了这些建议(上图) - 可能只是没有按照一切正确 - 所以我做了网络搜索,发现了什么对我有用'http://incise.org/android-development-on-the-command-line.html'。

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

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

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 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)第二方注意:上面提到的链接还包括从命令构建整个项目的指令。

希望这有助于解决这个问题。我知道我很高兴在这里找到关于这个主题的内容。


原文链接