简体   繁体   English

从命令行构建 Eclipse Java 项目

[英]Build Eclipse Java Project from Command Line

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.我正在尝试自动化我的构建(使用 FinalBuilder 而不是 ant),我既不是 Java 也不是 Eclipse 专家。 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.我可能可以弄清楚如何使用直接的 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?如果无法通过命令行编译 Eclipse 项目,有没有办法从 Eclipse 中生成所需的 java 命令行? Or are there some files I can poke around to find the compile steps it is doing behind the scenes?或者是否有一些文件我可以四处寻找它在幕后执行的编译步骤?


Guys, I'm looking for an answer that does NOT include 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:我不认为这是一个不合理的问题,因为我可以为视觉工作室做这样的事情:

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

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.它使用jdt apt插件自动构建您的工作区。 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:如果你没有使用 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 用“equinox 启动器”替换了startup.jar

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 版本。

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:为了完成 André 的回答,ant 解决方案可能类似于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>

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 here which would be invoked in a command line with:您可以在此处找到完整的 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.批处理编译器类位于 JDT Core 插件中。 The name of the class is org.eclipse.jdt.compiler.batch.BatchCompiler.类的名称是 org.eclipse.jdt.compiler.batch.BatchCompiler。 It is packaged into plugins/org.eclipse.jdt.core_3.4.0..jar.它被打包到 plugins/org.eclipse.jdt.core_3.4.0..jar 中。 Since 3.2, it is also available as a separate download.从 3.2 开始,它也可以单独下载。 The name of the file is ecj.jar.该文件的名称是 ecj.jar。
Since 3.3, this jar also contains the support for jsr199 (Compiler API) and the support for jsr269 (Annotation processing).从 3.3 开始,这个 jar 还包含对 jsr199(编译器 API)的支持和对 jsr269(注解处理)的支持。 In order to use the annotations processing support, a 1.6 VM is required.为了使用注释处理支持,需要 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 .与 Visual Studio 命令行编译功能的不同之处在于Eclipse 似乎并不直接在命令行参数中读取其 .project 和 .classpath 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.您必须在各种命令行选项中报告 .project 和 .classpath 中包含的所有信息,才能获得完全相同的编译结果。

So, then short answer is: "yes, Eclipse kind of does."所以,简短的回答是:“是的,Eclipse 确实如此。” ;) ;)

After 27 years, I too, am uncomfortable developing in an IDE. 27 年后,我也对在 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 '.我尝试了这些建议(上图)——可能只是没有正确地遵循一切——所以我进行了网络搜索,并在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.如上所述,eclipse/adt 不会创建必要的 ant 文件。 In order to compile without eclipse IDE (and without creating ant scripts):为了在没有 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. 2)接下来我编译项目。 I was a little confused by the request to not use 'ant'.我对不使用“ant”的要求有点困惑。 Hopefully -- requester meant that he didn't want to write any ant scripts.希望——请求者的意思是他不想编写任何 ant 脚本。 I say this because the next step is to compile the application using 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) 要在我的安卓手机上运行该项目,我使用 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 anything about this topic here.我知道我很高兴在这里找到有关此主题的任何信息。

The normal apporoach works the other way around: You create your build based upon maven or ant and then use integrations for your IDE of choice so that you are independent from it, which is esp.通常的方法是相反的:您基于mavenant创建构建,然后为您选择的 IDE 使用集成,以便您独立于它,尤其是。 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.我建议使用 maven 并让它为您完成繁重的工作。 Create a pom file and generate the eclipse project via mvn eclipse:eclipse.创建一个 pom 文件并通过 mvn eclipse:eclipse 生成 eclipse 项目。 HTH高温高压

This question contains 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 项目。

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.不太清楚为什么它显然做了两次,但它似乎有效。

I wanted to just add this comment to this question for anyone who may encounter this issue in the future.对于将来可能遇到此问题的任何人,我只想将此评论添加到此问题中。

Once, I was working on a project and building complex and inter-related JAVA projects from CMD (using ECLIPSE) was my only option.有一次,我在做一个项目,从 CMD(使用 ECLIPSE)构建复杂且相互关联的 JAVA 项目是我唯一的选择。 So, all you need to is this:所以,你需要做的就是:

  • Download your desired ECLIPSE IDE (tested it with ECLIPSE OXYGEN, Eclipse IDE for Enterprise Java and Web Developers and Eclipse IDE for Java Developers and it works fine)下载您想要的 ECLIPSE IDE(使用 ECLIPSE OXYGEN、适用于企业 Java 和 Web 开发人员的 Eclipse IDE 以及适用于 Java 开发人员的 Eclipse IDE 进行了测试,并且运行良好)

  • Open your project in the ECLIPSE and change the ECLIPSE configuration based on your project在 ECLIPSE 中打开您的项目并根据您的项目更改 ECLIPSE 配置

  • Close the ECLIPSE and save the .metadata created from your ECLIPSE (If your workspace is going to be refreshed in the future)关闭 ECLIPSE 并保存从 ECLIPSE 创建的 .metadata(如果您的工作区将来要刷新)

  • Run this script in the CMD:在 CMD 中运行此脚本:

"%ECLIPSE_IDE%\eclipsec.exe" -noSplash -data "YOUR_WORKSPACE" -application org.eclipse.jdt.apt.core.aptBuild

So, all I wanted to say is that, I could not build using the above commands without the .metadata being in the workspace.所以,我想说的是,如果没有 .metadata 在工作区中,我无法使用上述命令进行构建。 Make sure it exists there.确保它存在于那里。 If you need, make a copy of .metadata and copy-paste it each time your want to execute the commands.如果需要,请在每次要执行命令时复制 .metadata 并复制粘贴。

Hi Just addition to VonC comments.嗨,除了 VonC 评论。 I am using ecj compiler to compile my project.我正在使用 ecj 编译器来编译我的项目。 it was throwing expcetion that some of the classes are not found.一些课程没有找到,这让我感到很奇怪。 But the project was bulding fine with javac compiler.但是该项目使用 javac 编译器构建得很好。

So just I added the classes into the classpath(which we have to pass as argument) and now its working fine... :)所以我只是将类添加到类路径中(我们必须将其作为参数传递),现在它工作正常...... :)

Kulbir Singh库尔比尔辛格

Short answer.简短的回答。 No. Eclipse does not have a command line switch like Visual Studio to build a project.不。Eclipse 没有像 Visual Studio 这样的命令行开关来构建项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM