从命令行构建Eclipse Java项目

时间:2023-02-05 18:20:13

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 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:

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

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

8 个解决方案

#1


56  

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用“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启动器的版本号将取决于您拥有的日食版本。

#2


21  

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. 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就是这样。” ;)

#3


7  

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-命令line.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 anything about this topic here.

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

#4


3  

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. 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

正常的apporoach反过来工作:你基于maven或ant创建你的构建,然后使用你选择的IDE的集成,这样你就可以独立于它了,特别是。当您尝试使新团队成员加快速度或使用连续集成服务器进行自动构建时,这一点非常重要。我建议使用maven,让它为你做繁重的工作。创建一个pom文件并通过mvn eclipse:eclipse生成eclipse项目。 HTH

#5


2  

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项目。

#6


1  

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.

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

#7


0  

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

Kulbir Singh

#8


-3  

Short answer. No. Eclipse does not have a command line switch like Visual Studio to build a project.

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

#1


56  

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用“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启动器的版本号将取决于您拥有的日食版本。

#2


21  

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. 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就是这样。” ;)

#3


7  

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-命令line.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 anything about this topic here.

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

#4


3  

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. 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

正常的apporoach反过来工作:你基于maven或ant创建你的构建,然后使用你选择的IDE的集成,这样你就可以独立于它了,特别是。当您尝试使新团队成员加快速度或使用连续集成服务器进行自动构建时,这一点非常重要。我建议使用maven,让它为你做繁重的工作。创建一个pom文件并通过mvn eclipse:eclipse生成eclipse项目。 HTH

#5


2  

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项目。

#6


1  

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.

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

#7


0  

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

Kulbir Singh

#8


-3  

Short answer. No. Eclipse does not have a command line switch like Visual Studio to build a project.

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