简体   繁体   中英

Packaging common classes into WAR with maven and eclipse

I have two maven-based eclipse projects, one being a project with common classes and one a Java EE web application using these.

Now, I know the way to build the common project as a jar and add it as a dependency to the other.

But how would I solve it, if I want the common classes directly be included in the WAR's WEB-INF/classes?

And, will that mechanism work both within eclipse and from the command line running a maven command?

You can use the Maven dependency plugin to unpack your.jar artifact containing the common classes to the target folder. You would also need to make your original dependency have the provided scope, so the classes are not included in both, the classes folder and (as a.jar) in the libs folder inside your resulting war.

The correct way is to have two Maven projects. By declaring the packaging on your library as "jar" and the webapp as "war" maven will use the correct plugins to build your application as your described. Your scenario is fairly common, in addition to the information I'm about to give you, there is lots of info on this available in the Maven documentation.

Your library jar should be built with a pom similar to this:

<?xml version="1.0" encoding="UTF-8"?>
<project
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.myproject.mypackage</groupId>
    <artifactId>my-lib</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>${project.artifactId}</name>
    <packaging>jar</packaging>

Your webapp should be built with a pom similiar to this:

<?xml version="1.0" encoding="UTF-8"?>
<project
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.myproject.mypackage</groupId>
    <artifactId>my-war</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>${project.artifactId}</name>

    <dependencies>
        <dependency>
            <groupId>com.myproject.mypackage</groupId>
            <artifactId>my-lib</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>

When you build the webapp, maven will automatically include your library Jar in your web-inf/lib directory. Never ever place files manually in this directory, let Maven do the heavy lifting for you.

EDIT: The question was how to unpack a jar during the build, which is a very very bad idea for a multitude of reasons and bad practices. Persistence classes belong in an EJB, not in WEB-INF/classes. Doing this will also put you outside standard Java EE support... Nevertheless, here's the answer:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.4</version>
    <executions>
        <execution>
            <id>unpack</id>
            <phase>package</phase>
            <goals>
                <goal>unpack-dependencies</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>com.myproject.mypackage</groupId>
                        <artifactId>my-lib</artifactId>
                        <version>1.0.0-SNAPSHOT</version>
                        <overWrite>true</overWrite>
                        <outputDirectory>WEB-INF/classes</outputDirectory>
                    </artifactItem>
                </artifactItems>
            </configuration>
        </execution>
    </executions>
</plugin>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM