How to create war files
You can use Ant to set up, compile, WAR, and deploy your solution.
<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:
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
Set up
Define a setup
task that creates the distribution directory and copies any artifacts that need to be WARred directly:
<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 src
but need to be available at runtime (e.g. properties, XML files, etc.):
<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:
<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:
<target name="deploy">
<copy file="My.war" todir="${tomcat.deploydir}" />
</target>
Click and go!
Once all this is set up, simply launching the default
target from Eclipse will compile, WAR, and deploy your solution.
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.
Generate war file from tomcat webapp folder
You can create .war file back from your existing folder.
Using this command
cd /to/your/folder/location
jar -cvf my_web_app.war *
How to create a war file for my java web application project from terminal without using eclipse
In pom.xml, add the following line
<packaging>war</packaging> // Replace <packaging>jar</packaging> if jar packaging already present
And then go to the parent directory of the project and enter mvn package
. war file will be generated in
/parent_directory/target/myproject.war
If you want to access your project as http://localhost:8080
rather than http://localhost:8080/myproject
, rename myproject.war
to ROOT.war
and paste the war file in webapps of tomcat directory.
How to create a WAR file using the commandline?
Alright here is a already existing SO with a couple of possibilities on how to create a war file: How to create war files.
I personaly prefer Maven over any other build scenario - Since i migrated from ANT i did not like Maven in the first place but after investing like a day or two into the documentations: http://maven.apache.org/pom.html i started to love it. In your case all you need is refactor your project to meet the maven standard file structure and create a minimum Maven project (a.k.a. pom.xml
file) for your sources that has the packaging war
instead of the default jar
.
If you want to stick with the console only and dont use any build helpers beside the ones delivered by the JDK you can create your war with something like this:
jar cvf my-app.war
Have a look at this: https://docs.oracle.com/javase/tutorial/deployment/jar/build.html for a complete reference of the supported options.
(Note: A "war" in the context of java based web applications follows a standard format: http://docs.oracle.com/javaee/6/tutorial/doc/bnaby.html while the technical ".war" file is just the same as a ZIP compressed folder renamed to ".zip".)
(Also note i would realy take the time to get in touch with maven since after that you know about a serious build tool and will be able to create jar
, ear
, war
etc. in a standard process).
Related Topics
Try with Resources VS Try-Catch
Java Jar File: Use Resource Errors: Uri Is Not Hierarchical
How Do Hashcode() and Identityhashcode() Work at the Back End
Valid Characters in a Java Class Name
Alert Handling in Selenium Webdriver (Selenium 2) with Java
Java Calendar, Date, and Time Management for a Multi-Timezone Application
Why to Use Swingutilities.Invokelater in Main Method
Method Overloading and Choosing the Most Specific Type
How to Replace the Awt Eventqueue with Own Implementation
Scanner Nosuchelementexception
Why Won't This Generic Java Code Compile
How to Show/Hide a Column at Runtime
How to Import Org.Openqa.Selenium.Webdriver Using Selenium and Java 11
Invoking a Static Method Using Reflection
Difference Between Spring @Controller and @Restcontroller Annotation