如何在 Eclipse 中自动创建(脚本)war 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/293900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 13:24:32  来源:igfitidea点击:

How can I automate (script) creating a war file in eclipse?

eclipsewar

提问by stu

It's 5 button clicks to get eclipse to create a deployable war file for my eclipse project, I figure there's probably some eclipse command line option to do the same thing, so I can just write it into a script, but I'm not seeing it.

点击 5 次按钮让 eclipse 为我的 eclipse 项目创建一个可部署的 war 文件,我想可能有一些 eclipse 命令行选项可以做同样的事情,所以我可以把它写成一个脚本,但我没有看到它.

采纳答案by Jon Skeet

Use the Ant wartask, set up a relevant build file and you can just hit the "external tools" button to execute it.

使用Antwar任务,设置相关的构建文件,您只需点击“外部工具”按钮即可执行它。

回答by Mattias Holmqvist

You could also setup a Maven build for your web project. Typing mvn package from the command line would then build the project for you.

您还可以为您的 Web 项目设置 Maven 构建。从命令行输入 mvn package 然后将为您构建项目。

For integration between Maven and Eclipse, see m2Eclipseand Maven Eclipse Plugin.

有关 Maven 和 Eclipse 之间的集成,请参阅m2EclipseMaven Eclipse 插件

回答by Bananeweizen

I cannot say anything about the WAR packaging itself, sorry.

我不能说 WAR 包装本身,抱歉。

But as I wrote in How do I automatically export a WAR after Java build in Eclipse?: If youcan describe the WAR packaging with an Ant script, you can have that Ant script being executed automatically after each change to your project. Use Project->Properties->Builders->Add->Ant Builder. Give that builder you custom Ant script and it will automatically be executed after the "normal" builders of your project. You can even specify in the settings of the builder, if it shall only react on changes to specific files and so on.

但正如我在 Eclipse 中构建 Java 后如何自动导出 WAR 中所写的 那样?:如果可以使用 Ant 脚本来描述 WAR 打包,那么您可以在每次更改项目后自动执行该 Ant 脚本。使用 Project->Properties->Builders->Add->Ant Builder。为该构建器提供自定义 Ant 脚本,它将在项目的“普通”构建器之后自动执行。您甚至可以在构建器的设置中指定,如果它只对特定文件的更改等做出反应。

The Ant builder is kind of a Swiss army knife for anything you want to automate in the project build without having to use the big tools like maven.

Ant builder 就像一把瑞士军刀,可以在项目构建中自动执行任何操作,而无需使用像 maven 这样的大工具。

回答by Honza

This Ant script should work for standard Dynamic Web Project structure of project:

这个 Ant 脚本应该适用于项目的标准动态 Web 项目结构:

Create Ant build.xml with replacing of two properties at begining:

创建 Ant build.xml 并在开始时替换两个属性:

<?xml version="1.0" encoding="UTF-8"?>
<project name="Deploy From Eclipse to JBoss" basedir="." default="deploy">

  <!-- This replace with yours project name and JBoss location: -->
  <property name="warfile" value="MyProject"/>
  <property name="deploy" value="/home/honza/jboss-as-7.1.1.Final/standalone/deployments"/>

  <target name="create">
    <war destfile="${warfile}.war" webxml="WebContent/WEB-INF/web.xml" update="true">
      <classes dir="build\classes"/>
      <fileset dir="WebContent">
        <exclude name="WEB-INF/web.xml"/>
      </fileset>
    </war>
  </target>
  <target name="copy">
    <copy todir="${deploy}" overwrite="true">
      <fileset dir=".">
        <include name="${warfile}.war"/>
      </fileset>
    </copy>
  </target>
  <target name="clear">
    <delete includeemptydirs="true">
      <fileset dir="${deploy}" defaultexcludes="false">
        <include name="${warfile}.*/**" />
      </fileset>
    </delete>
  </target>
  <target name="deploy">
    <antcall target="create"/>
    <antcall target="clear"/>
    <antcall target="copy"/>
  </target>
</project>

Now should command "ant" do WAR creation and copy them to the JBoss. JBoss automatically deploys wars which finds in deployment directory.

现在应该命令“ant”创建 WAR 并将它们复制到 JBoss。JBoss 自动部署在部署目录中找到的War。

For automatic run after build (Project - Build) add this Buildfile here:

要在构建后自动运行(项目 - 构建),请在此处添加此构建文件:

MyProject - Properties - New - Ant builder