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

I am new to Maven, so I apologize if this is something trivial. Basically, I develop webapp, and I use Maven to manage the project. I have <packaging>war</packaging>in my pom.xml, so when I run it mvn package, it will spit out a war file for deployment on a web server.

Now, as part of this application, we use a third-party library, which is delivered to us as a war, and it is deployed separately on a web server. This war includes some custom integration features that we encode. For persistence logic, I initially just wrote a repository right in this integration code. Since I find that I need more stability logic (more than basic SELECTs), I find that I want to use the repositories and domain objects found in our application code. Therefore, ideally, I would like to be able to break our main packages, and then include this bank in this third-party war, so that I have the same functionality as I do there.

I just don’t know how I can configure pom.xml to tell which packages I need in this bank (or even just 1 package if necessary), and how to create the bank itself. Is there a way to generate a jar from specific packages in a project that will configure the package for the entire project as a war?

I found information about the Maven jar plugin , but it says: "If the packaging for your project is set to" jar ", this plugin runs whenever it goes through the" package "phase." Packaging in my project is not installed in the bank. Is there any way to use this plugin?

+5
source share
4 answers

, , - -. , war , jar.

:

.
├── 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>

/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>

jar domain.


Overlays, war, war war. , , . .

+4

, jar , pom java/web, java. java, , xyz-common, , . mvn, xyz-common jar . pom , .

, java ? , java Java- . jar.

+2

Use maven-jar-plugin, see

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
source

Have you tried to generate a project with a team mvn archetype:generate? He creates a fully working project structure for webapps with military packaging:

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp
0
source

All Articles