Generate .war file from web app containing just HT

2019-03-27 15:45发布

问题:

I am trying to create a war file that will be deployed on a web/application server.

The source files of the app are purely HTML, CSS, and JavaScript. There is a separate war file for our REST API and for the rest of our backend code.

Most of the guides out there talk about using java to compile, and pointing to WEB-INF folders etc.

However, as I mentioned, in the HTML/CSS/JS war, I don't use any Java, don't use WEB-INF, and there are no servlets or other things you would normally see in a "Java" war file.

How do I compile or create this type of war file?

The contents look like this:

WebContent/HTML WebContent/CSS WebContent/JS

All libraries for JavaScript contained within JS folder.

Would this work: Simply run:

src.dist="./WebContent"
app.name="example"
app.version=1
work.home="./temp"

jar jarfile="${src.dist}/${app.name}-${app.version}.war"
     basedir="${work.home}"

Obviously I would have set up the rest of the script correctly.

回答1:

This is extremely simple:

  1. Create a folder
  2. Add a src/main/webapp folder
  3. Add all of your HTML, CSS and JS files to the src/main/webapp folder
  4. Add an empty web.xml file to the src/main/webapp/WEB-INF directory.
  5. add a maven pom.xml
  6. add the maven-war-plugin to your pom.xml, with the following configuration:

    <!--  create the war -->
    <plugin>
      <artifactId>maven-war-plugin</artifactId>
      <configuration>
        <webXml>src/main/webapp/WEB-INF/web.xml</webXml>
      </configuration>
    </plugin>
    
  7. run mvn clean install!



回答2:

If you are creating a war file, then you are deploying on a Java based web application server, something like Tomcat or Wildfly.

If you are using eclipse, you can do so by New > Dynamic Web Project (maybe name it foo-bar), click next, next and finish. Then open that foo-bar project and create your css and js folders under WebContent like so.

\WebContent\css
\WebContent\js
\WebContent\index.html
\WebContent\foo-bar.html

You can right click the foo-bar project > Export > Web WAR file.

When you deploy it on say Tomcat, you can test to access your static content like so

http://localhost:8080/foo-bar/css/foo-bar.css
http://localhost:8080/foo-bar/js/foo-bar.js
http://localhost:8080/foo-bar/foo-bar.html

Hope this helps.