I am working on a Java Web App project. This project has a fixed file structure and does not use Maven. I want to generate a WAR file so that I can deploy it to Tomcat.
I want to generate the WAR without Eclipse, just via the Java tool chain. Is this possible?
I have put together a minimal reproducible example with the same file structure in a git repo at https://github.com/halloleo/ContextListenerJavaWebAppSampleProject.
CodePudding user response:
A WAR file is a archive (zip/jar) that follows a certain structure. See here for more details.
If you want to create a WAR manually you will need to create the structure manually and then create an archive with the extension of .war
.
Here is a quick and dirty ant script that can create a WAR file from your structure posted on GitHub
<!-- Compile the classes and copy other files to conform to the WAR structure -->
<target name="compile">
<mkdir dir="build/WEB-INF/classes" />
<copy file="WebContent/index.jsp" todir="build" />
<copydir src="WebContent/WEB-INF/" dest="build/WEB-INF/" />
<javac srcdir="src" destdir="build/WEB-INF/classes" classpath="WebContent/WEB-INF/lib/javax.servlet-api-4.0.1.jar" />
</target>
<!-- Create the WAR file -->
<target name="war">
<mkdir dir="dist" />
<war destfile="dist/test.war" basedir="build" webxml="WebContent/WEB-INF/web.xml"/>
</target>
Save this as build.xml
and run ant clean compile dist
to generate the WAR file in dist
directory.
NOTE: You may have to install the ant tool if you already dont have it. Also, if you want to go complete tool free - you can read the build.xml
file and convert the steps into a 'batch' / 'shell' script (all the steps are self explanatory).
CONCLUSION: Hope this helps. But I still recommend using a tool like Maven or Gradle these will come in handy when the project grows (specially when there are several libraries along with their dependencies)