How to create a war and jar from a web project?

I have a maven web project that has packaging as "war". the package generates war file that confirms to war format. But, i like to package the project as a "jar" as well along with the default "war" format. So, I am trying to use "maven jar" plugin to achieve that. I am using the following jar plugin configuration.

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <executions>
                <execution>
                    <id>make-a-jar</id>
                    <phase>package</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <classesDirectory>${project.build.outputDirectory}</classesDirectory>
                <includes>
                    <include>../../src/main/webapp/**</include>
                    <include>**/*</include>
                </includes>
            </configuration>
        </plugin>

The "package" command produces a jar file that contains the classes and the contents of 'resources' folder and war file produces a proper 'war' file.

But the contents of 'src/main/webapp' are not included in the jar file. How to include the contents of src/main/webapp in the jar using the jar plugin?


Normally the Maven war:war provides 2 significant parameter as the following: -

1 archiveClasses

Whether a JAR file will be created for the classes in the webapp. Using this optional configuration parameter will make the compiled classes to be archived into a JAR file and the classes directory will then be excluded from the webapp. Default value is: false. User property is : archiveClasses.

2 attachClasses

Whether classes (that is the content of the WEB-INF/classes directory) should be attached to the project as an additional artifact. By default the classifier for the additional artifact is 'classes'. You can change it with the <classesClassifier>someclassifier</classesClassifier> parameter.

Even this is a feature provides the Maven War Plugin, this does not include the src/main/webapp to that created jar file. Then you may consider the Maven Asssembly Plugin

The Assembly Plugin for Maven is primarily intended to allow users to aggregate the project output along with its dependencies, modules, site documentation, and other files into a single distributable archive. Currently it can create distributions in the following formats:

  • zip
  • tar
  • tar.gz
  • tar.bz2
  • jar
  • dir
  • war
  • and any other format that the ArchiveManager has been configured for
  • By using the Assembly Descriptors, you can Filtering Some Distribution Files and Including and Excluding Artifacts and so on. This means you are able to create your own archive with any required contents/resources.

    If you would like to use the Maven Jar Plugin as you've mentioned. It also provides the following:-

  • To Include/Exclude content from jar artifact
  • To create an additional attached jar artifact from the project
  • etc.
  • The usage page will give you further information.

    I hope this may help.


    The only clean way to do that with Maven is two split the project in two project, one for building the jar, one for building the war.(more simple standard projects are better than one big non standard project, especially with Maven)

    Now If you want also to share the files in src/main/webapp, you can either use war overlays (war as dependencies), or use something like the spring servletDispatcher and its mvc:resources tags.

    链接地址: http://www.djcxy.com/p/74656.html

    上一篇: 把xhtml页面放到一个jar maven项目中

    下一篇: 如何从Web项目创建一个war和jar?