How to create a jar from part of my project that is packaged as a war

前端 未结 4 1482
旧时难觅i
旧时难觅i 2020-12-07 02:55

I\'m a Maven newbie, so I apologize if this is something trivial. Basically, I am developing a webapp, and I am using Maven to manage the project. I have

相关标签:
4条回答
  • 2020-12-07 03:27

    Use maven-jar-plugin, see :

    • http://maven.apache.org/plugins/maven-jar-plugin/usage.html

    Example:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
                        <configuration>
                            <includes>
                                 <include>**/service/*</include>
                            </includes>
                        </configuration>
            <executions>
                <execution>
                    <id>make-a-jar</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    
    0 讨论(0)
  • 2020-12-07 03:33

    You can create a multi module project where one module contains your domain classes and the other module is your web application. By doing so your external 3rd party war can use your domain classes by just include that jar.

    This is a simple overview of the directory structure:

    .
    ├── pom.xml
    ├── domain
    |   ├── pom.xml
    |   └── src
    |       └── main
    |           └── java
    |               └── com
    |                   └── stackoverflow
    |                       └── domain
    |                           ├── SomeDao.java
    |                           └── AnotherDao.java
    └── web
        ├── pom.xml
        └── src
            └── main
                ├── java
                |   └── com
                |       └── stackoverflow
                |           └── web
                |               └── SomeBackingBean.java
                └── webapp
                    └── WEB-INF
                        └── web.xml
    

    pom.xml

    <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/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.stackoverflow</groupId>
        <artifactId>Q12576767</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>pom</packaging>
    
        <name>${project.artifactId}-${project.version}</name>
    
        <modules>
            <module>domain</module>
            <module>web</module>
        </modules>
    
        <dependencyManagement>
            <dependencies>
                <!-- Inter-Module dependencies -->
                <dependency>
                    <groupId>com.stackoverflow</groupId>
                    <artifactId>Q12576767-domain</artifactId>
                    <version>${project.version}</version>
                </dependency>
            </dependencies>
        </dependencyManagement>
    </project>
    

    domain/pom.xml

    <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/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <parent>
            <groupId>com.stackoverflow</groupId>
            <artifactId>Q12576767</artifactId>
            <version>1.0-SNAPSHOT</version>
        </parent>
    
        <artifactId>Q12576767-domain</artifactId>
    
        <name>${project.artifactId}-${project.version}</name>
    </project>
    

    web/pom.xml

    <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/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <parent>
            <groupId>com.stackoverflow</groupId>
            <artifactId>Q12576767</artifactId>
            <version>1.0-SNAPSHOT</version>
        </parent>
    
        <artifactId>Q12576767-web</artifactId>
        <packaging>war</packaging>
    
        <name>${project.artifactId}-${project.version}</name>
    
        <dependencies>
            <dependency>
                <groupId>com.stackoverflow</groupId>
                <artifactId>Q12576767-domain</artifactId>
            </dependency>
        </dependencies>
    </project>
    

    By doing it this way you decouple some dependencies and can reuse the jar file from the domain module.


    Also at the end you can look at using Overlays to just create one war by overlaying the 3rd-party war with your own war. I don't know if it is feasible in your setup but it is worth looking at. I have sucessfully used it.

    0 讨论(0)
  • 2020-12-07 03:40

    Have you tried to generate the project with the mvn archetype:generate command? It builds you a fully working project structure for webapps with war packaging:

    mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp
    
    0 讨论(0)
  • 2020-12-07 03:54

    I dont think you can create a jar file from some packages because pom file belongs to one java/web project and not for individual packages in java project. You can create a new java project named e.g xyz-common which should have the code which will be shared with other war files. Then you can simple do mvn install and your xyz-common jar will be created with some version. Then update the pom file of the war file which needs your common jar with its dependency.

    I assume your java packages are inside the war file? If so, better create a new java project and don't tightly couple your java code with war file. In your war file, just mention the dependency of your jar file.

    0 讨论(0)
提交回复
热议问题