从命令行构建Eclipse Java项目

Build Eclipse Java Project from Command Line

有没有一种方法可以从命令行编译基于Eclipse的Java项目?

我正在尝试使我的构建自动化(使用FinalBuilder而不是ant),而且我既不是Java专家也不是Eclipse专家。 我可能可以弄清楚如何使用直接的Java命令行选项来执行此操作,但是Eclipse项目感觉像是在浪费很多精力。

如果无法通过命令行编译Eclipse项目,是否可以从Eclipse内部生成所需的Java命令行? 还是有一些我可以四处查找的文件来查找其在后台执行的编译步骤?

伙计们,我正在寻找一个不包含蚂蚁的答案。 让我再次重申原始问题.......是否可以通过命令行构建Eclipse项目?

考虑到我可以为Visual Studio做这样的事情,我认为这不是一个不合理的问题:

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


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

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

它使用jdt apt插件自动构建您的工作区。这也称为"无头构建"。该死的很难弄清楚。如果您没有使用Win32 exe,则可以尝试以下操作:

1
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火星(MacOX)上:

1
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参数指定工作空间的位置。

春分发射器的版本号将取决于您所使用的Eclipse版本。


为了完成André的回答,一个ant解决方案可能类似于Emacs,JDEE,Ant和Eclipse Java Compiler中描述的解决方案,如下所示:

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

Compilerarg元素还允许您将其他命令行args传递给Eclipse编译器。

您可以在此处找到完整的ant脚本示例,该示例将在命令行中使用以下命令进行调用:

1
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

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

有关批处理编译,请参阅编译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.

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

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

要么:

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

该部分还将详细介绍所有Java编译选项。

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

因此,简短的答案是:"是的,Eclipse确实可以做到。" ;)


27年后,我也对使用IDE感到不舒服。我尝试了这些建议(以上)-可能只是没有正确地遵循所有建议-因此我进行了一次网络搜索,并在'http://incise.org/android-development-on-the- command-line.html"。

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

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

1)在顶层目录中生成build.xml:

1
2
3
4
5
6
7
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编译应用程序

1
2
3
4
5
 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的设备上:

1
2
3
 ant target install

    ** my command line looked like  ant debug install

4)要在我的Android手机上运行项目,请使用adb。

1
2
3
4
5
6
 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)旁注:要使用设备查看日志,请执行以下操作:

1
 adb logcat

3B)第二点注意:上面提到的链接还包括用于从命令构建整个项目的说明。

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


正常的工作方式是相反的:您基于maven或ant创建构建,然后将集成用于您选择的IDE,从而使它独立于esp。当您尝试使新的团队成员加快工作速度或使用竞争激烈的集成服务器进行自动构建时,这一点很重要。我建议使用Maven,让它为您完成繁重的工作。创建一个pom文件并通过mvn eclipse:eclipse生成eclipse项目。高温超导


这个问题包含无头构建中的一些有用链接,但它们主要用于构建插件。我不确定其中有多少可以应用于纯Java项目。


只是想加我两分钱。我尝试按照@Kieveli针对非win32的建议进行操作(以下重复),但对我不起作用(在带有Eclipse的CentOS上:Luna):

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

在我使用Eclipse(Luna)在CentOS上进行的特殊设置中,此方法有效:

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

输出应如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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编译器来编译我的项目。有人期望找不到某些类。但是该项目使用javac编译器时效果很好。

因此,仅将类添加到类路径中(我们必须将其作为参数传递),现在它可以正常工作了... :)

库尔伯·辛格


简短的答案。没有。
Eclipse没有像Visual Studio这样的命令行开关来构建项目。