Java 如何创建war文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1001714/
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-08-11 22:06:56  来源:igfitidea点击:

How to create war files

javaeclipsetomcatwar

提问by

What are the best practices of creating war files (using eclipse) to run on tomcat? tutorials, links, examples are highly appreciated.

创建war文件(使用eclipse)以在tomcat上运行的最佳实践是什么?教程、链接、示例受到高度赞赏。

采纳答案by David Citron

You can use Antto set up, compile, WAR, and deploy your solution.

您可以使用Ant来设置、编译、WAR和部署您的解决方案。

<target name="default" depends="setup,compile,buildwar,deploy"></target>

You can then execute one click in Eclipse to run that Ant target. Here are examples of each of the steps:

然后,您可以在 Eclipse 中执行一次单击以运行该 Ant 目标。以下是每个步骤的示例:

Preconditions

先决条件

We'll assume that you have your code organized like:

我们假设您的代码组织如下:

  • ${basedir}/src: Java files, properties, XML config files
  • ${basedir}/web: Your JSP files
  • ${basedir}/web/lib: Any JARs required at runtime
  • ${basedir}/web/META-INF: Your manifest
  • ${basedir}/web/WEB-INF: Your web.xml files
  • ${basedir}/src:Java 文件、属性、XML 配置文件
  • ${basedir}/web: 你的 JSP 文件
  • ${basedir}/web/lib: 运行时需要的任何 JAR
  • ${basedir}/web/META-INF: 你的清单
  • ${basedir}/web/WEB-INF:您的 web.xml 文件

Set up

设置

Define a setuptask that creates the distribution directory and copies any artifacts that need to be WARred directly:

定义一个setup任务来创建分发目录并直接复制任何需要 WARred 的工件:

<target name="setup">
    <mkdir dir="dist" />
    <echo>Copying web into dist</echo>
    <copydir dest="dist/web" src="web" />
    <copydir dest="dist/web/WEB-INF/lib" src="${basedir}/../web/WEB-INF/lib" />
</target>

Compile

编译

Build your Java files into classes and copy over any non-Java artifacts that reside under srcbut need to be available at runtime (e.g. properties, XML files, etc.):

将您的 Java 文件构建到类中,并复制驻留在下面src但需要在运行时可用的任何非 Java 工件(例如属性、XML 文件等):

<target name="compile">
    <delete dir="${dist.dir}/web/WEB-INF/classes" />
    <mkdir dir="${dist.dir}/web/WEB-INF/classes" />
    <javac destdir="${dist.dir}/web/WEB-INF/classes" srcdir="src">
        <classpath>
            <fileset dir="${basedir}/../web/WEB-INF/lib">
                  <include name="*" />
            </fileset>
        </classpath>
    </javac>
    <copy todir="${dist.dir}/web/WEB-INF/classes">
        <fileset dir="src">
            <include name="**/*.properties" />
            <include name="**/*.xml" />
        </fileset>
    </copy>
</target>

Build WAR

建立战争

Create the WAR itself:

创建 WAR 本身:

<target name="buildwar">
    <war basedir="${basedir}/dist/web" destfile="My.war"
     webxml="${basedir}/dist/web/WEB-INF/web.xml">
        <exclude name="WEB-INF/**" />
        <webinf dir="${basedir}/dist/web/WEB-INF/">
            <include name="**/*.jar" />
        </webinf>
    </war>
</target>

Deploy

部署

Finally, you can set up a task to deploy the WAR directly into your Tomcat deploy location:

最后,您可以设置一个任务,将 WAR 直接部署到您的 Tomcat 部署位置:

<target name="deploy">
    <copy file="My.war" todir="${tomcat.deploydir}" />
</target>

Click and go!

点击走!

Once all this is set up, simply launching the defaulttarget from Eclipse will compile, WAR, and deploy your solution.

设置完所有这些后,只需default从 Eclipse启动目标即可编译、WAR 和部署您的解决方案。

The advantage of this approach is that it will work outside Eclipse as well as within Eclipse and can be used to easily share your deployment strategy (e.g. via source control) with other developers who are also working on your project.

这种方法的优点是它既可以在 Eclipse 之外也可以在 Eclipse 内工作,并且可以用来与其他也在处理您的项目的开发人员轻松共享您的部署策略(例如,通过源代码控制)。

回答by Mech Software

I've always just selected Export from Eclipse. It builds the war file and includes all necessary files. Providing you created the project as a web project that's all you'll need to do. Eclipse makes it very simple to do.

我一直只选择从 Eclipse 导出。它构建战争文件并包括所有必要的文件。如果您将项目创建为 Web 项目,这就是您需要做的全部工作。Eclipse 使它变得非常简单。

回答by Ilya Boyandin

回答by mikek

We use Maven (Ant's big brother) for all our java projects, and it has a very nifty WAR plugin. Tutorials and usage can be found there.

我们将 Maven(Ant 的老大哥)用于我们所有的 Java 项目,它有一个非常漂亮的 WAR 插件。可以在那里找到教程和用法。

It's a lot easier than Ant, fully compatible with Eclipse (use maven eclipse:eclipseto create Eclipse projects) and easy to configure.

比 Ant 简单很多,完全兼容 Eclipse(使用maven eclipse:eclipse创建 Eclipse 项目)并且易于配置。

Maven's homepage

Maven的主页

Maven WAR plugin

Maven WAR 插件

Sample Configuration:

示例配置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.1-alpha-2</version>
    <configuration>
        <outputDirectory>${project.build.directory}/tmp/</outputDirectory>
        <workDirectory>${project.build.directory}/tmp/war/work</workDirectory>
        <webappDirectory>${project.build.webappDirectory}</webappDirectory>
        <cacheFile>${project.build.directory}/tmp/war/work/webapp-cache.xml</cacheFile>
        <nonFilteredFileExtensions>
            <nonFilteredFileExtension>pdf</nonFilteredFileExtension>
            <nonFilteredFileExtension>png</nonFilteredFileExtension>
            <nonFilteredFileExtension>gif</nonFilteredFileExtension>
            <nonFilteredFileExtension>jsp</nonFilteredFileExtension>
        </nonFilteredFileExtensions>
        <webResources>
            <resource>
                <directory>src/main/webapp/</directory>
                <targetPath>WEB-INF</targetPath>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </webResources>
        <warName>Application</warName>
    </configuration>
</plugin>

回答by boxofrats

A war file is simply a jar file with a war extension, but what makes it work is how the contents is actually structured.

war 文件只是一个带有war 扩展名的jar 文件,但使其工作的是内容的实际结构。

The J2EE/Java EE tutorial can be a start:

J2EE/Java EE 教程可以作为一个开始:

http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/WebComponents3.html

http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/WebComponents3.html

And the Servlet specification contains the gory details:

Servlet 规范包含了血腥的细节:

http://java.sun.com/products/servlet/download.html

http://java.sun.com/products/servlet/download.html

If you create a new web project in Eclipse (I am referring to the Java EE version), the structure is created for you and you can also tell it where your Appserver is installed and it will deploy and start the application for you.

如果您在 Eclipse 中创建一个新的 Web 项目(我指的是 Java EE 版本),则为您创建了结构,您还可以告诉它您的 Appserver 的安装位置,它将为您部署和启动应用程序。

Using the "Export->WAR file" option will let you save the war file.

使用“Export->WAR file”选项可以让你保存war文件。

回答by Pablojim

If you are not sure what to do and are starting from scratch then Maven can help get you started.

如果您不确定该做什么并且要从头开始,那么 Maven 可以帮助您入门。

By following the the below steps you can get a new war project setup perfectly in eclipse.

通过遵循以下步骤,您可以在 eclipse 中完美地设置新的战争项目。

  1. Download and install Maven
  2. Go the command line run: mvn archetype:generate
  3. Follow the prompted steps - choosing the simple java web project (18) and a suitable name.
  4. When it is finished run: mvn eclipse:eclipse
  5. Start Eclipse. Choose File -> Import -> Existing project. Select the directory where you ran the mvn goals.
  6. That's it you should now have a very good start to a war project in eclipse
  7. You can create the war itself by running mvn packageor deploy it by setting up a server in eclipse and simply adding adding the project to the server.
  1. 下载并安装Maven
  2. 去命令行运行: mvn archetype:generate
  3. 按照提示的步骤操作 - 选择简单的 java web 项目 (18) 和合适的名称。
  4. 完成后运行: mvn eclipse:eclipse
  5. 启动日食。选择文件 -> 导入 -> 现有项目。选择运行 mvn 目标的目录。
  6. 就是这样,你现在应该有一个很好的开始在 eclipse 中的战争项目
  7. 您可以通过mvn package在 eclipse 中设置服务器并简单地将项目添加到服务器来运行或部署它来创建战争本身。

As some others have said the downside of using maven is that you have to use the maven conventions. But I think if you are just starting out, learning the conventions is a good idea before you start making your own. There's nothing to stop you changing/refactoring to your own preferred method at a later point.

正如其他一些人所说,使用 maven 的缺点是您必须使用 maven 约定。但我认为,如果您刚刚开始,在开始制作自己的约定之前,学习约定是个好主意。没有什么可以阻止您稍后更改/重构为您自己喜欢的方法。

Hope this helps.

希望这可以帮助。

回答by sms

Use ant build code I use this for my project SMS

使用 ant 构建代码我将它用于我的项目 SMS

<property name="WEB-INF" value="${basedir}/WebRoot/WEB-INF" />
<property name="OUT" value="${basedir}/out" />
<property name="WAR_FILE_NAME" value="mywebapplication.war" />
<property name="TEMP" value="${basedir}/temp" />

<target name="help">
    <echo>
        --------------------------------------------------
        compile - Compile
        archive - Generate WAR file
        --------------------------------------------------
    </echo>
</target>

<target name="init">
    <delete dir="${WEB-INF}/classes" />
    <mkdir dir="${WEB-INF}/classes" />
</target>

<target name="compile" depends="init">
    <javac srcdir="${basedir}/src" 
                destdir="${WEB-INF}/classes" 
                classpathref="libs">
    </javac>
</target>

<target name="archive" depends="compile">
    <delete dir="${OUT}" />
    <mkdir dir="${OUT}" />
    <delete dir="${TEMP}" />
    <mkdir dir="${TEMP}" />
    <copy todir="${TEMP}" >
        <fileset dir="${basedir}/WebRoot">
        </fileset>
    </copy>
    <move file="${TEMP}/log4j.properties" 
                    todir="${TEMP}/WEB-INF/classes" />
    <war destfile="${OUT}/${WAR_FILE_NAME}" 
                    basedir="${TEMP}" 
                    compress="true" 
                    webxml="${TEMP}/WEB-INF/web.xml" />
    <delete dir="${TEMP}" />
</target>

<path id="libs">
    <fileset includes="*.jar" dir="${WEB-INF}/lib" />
</path>

回答by arpadf

Another option would be to build it automatically using Eclipse. Of course if you have continuous integration environment Ant or Maven is recommended. The export alternative is not very convenient because you have to configure every time the export properties.

另一种选择是使用 Eclipse 自动构建它。当然,如果你有持续集成环境,推荐使用 Ant 或 Maven。导出替代方案不是很方便,因为您必须每次都配置导出属性。

STEPS:

脚步:

  1. Enable "Project Archives" support; this might depend on your project (I used it on Java EE/Web project). Right-click project root directory; Configure -> Add Project Archives Support.

  2. Go and create a new archive in the "Project Archives" top dir. You have only jar option, but name you archive *.war.

  3. Configure Fileset-s, i.e what files to be included. Typical is to configure two filesets similar how the Web Deployment Assembly (project property) is configured.

    • copy /WebContent to /
    • copy /build/classes to WEB-INF/classes (create this fileset after you define the WEB-INF/classes directory in the archive)
  4. You might need to tweek the fileset exclude property depending where you placed some of the config files or you might need more filesets, but the idea is that once you configured this you don't need to change it.

  5. Build the archive manually or publish directly to server; but is also automatically built for you by Eclipse

  1. 启用“项目档案”支持;这可能取决于您的项目(我在 Java EE/Web 项目中使用过它)。右键项目根目录;配置 -> 添加项目档案支持。

  2. 在“项目档案”顶部目录中创建一个新档案。您只有 jar 选项,但将存档命名为 *.war。

  3. 配置Fileset-s,即要包含哪些文件。典型的是配置两个文件集,类似于 Web 部署程序集(项目属性)的配置方式。

    • 将 /WebContent 复制到 /
    • 将 /build/classes 复制到 WEB-INF/classes(在存档中定义 WEB-INF/classes 目录后创建此文件集)
  4. 您可能需要根据您放置一些配置文件的位置调整文件集排除属性,或者您可能需要更多文件集,但这个想法是,一旦您配置了它,您就不需要更改它。

  5. 手动构建存档或直接发布到服务器;但也由 Eclipse 自动为您构建

回答by Charlie Dalsass

Another common option is gradle.

另一个常见的选择是 gradle。

http://www.gradle.org/docs/current/userguide/application_plugin.html

http://www.gradle.org/docs/current/userguide/application_plugin.html

To build your war file in a web app:

要在 Web 应用程序中构建您的 war 文件:

In build.gradle, add:

在 build.gradle 中,添加:

apply plugin: 'war'

Then:

然后:

./gradlew war

Use the layout from accepted answer above.

使用上面接受的答案中的布局。

回答by Ratnesh Kumar

**Making War file in Eclips Gaynemed of grails web project **

**在 grails web 项目的 Eclips Gaynemed 中制作 War 文件 **

1.Import project:

1.进口项目:

2.Change the datasource.groovy file

2.更改datasource.groovy文件

Like this: url="jdbc:postgresql://18.247.120.101:8432/PGMS"

像这样: url="jdbc:postgresql://18.247.120.101:8432/PGMS"

2.chnge AppConfig.xml

2.更改AppConfig.xml

3.kill the Java from Task Manager:

3.从任务管理器中杀死Java:

  1. run clean comand in eclips

  2. run 'prod war' fowllowed by project name.

  3. Check the log file and find the same .war file in directory of workbench with same date.

  1. 在 eclips 中运行 clean 命令

  2. 按项目名称运行“prod war”。

  3. 检查日志文件并在工作台目录中找到相同日期的相同 .war 文件。