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

2020-06-23 07:17发布

问题:

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?

回答1:

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:

  1. zip
  2. tar
  3. tar.gz
  4. tar.bz2
  5. jar
  6. dir
  7. war
  8. 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:-

  1. To Include/Exclude content from jar artifact
  2. To create an additional attached jar artifact from the project
  3. etc.

The usage page will give you further information.

I hope this may help.



回答2:

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.



标签: java maven