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 <packaging>war</packaging>
in my pom.xml so that that when I run mvn package
, it will spit out a war file to deploy on the web server.
Now, as part of this application, we are using a 3rd party library that is delivered to us as a war and it is deployed separately on the web server. This war includes some custom integration functionality that we code up. For the persistence logic, I originally just wrote up a repository straight in this integration code. As I find I'm needing more persistence logic (more than basic SELECTs), I'm finding I'm wanting to use repositories and domain objects found in our application code. So, ideally, I would like to be able to jar up our core packages, and then include that jar in this 3rd party war so I have the same functionality available to me there.
I just don't know how I could set up the pom.xml to tell it what packages I would want in this jar (or even just 1 package if necessary), and how to create the jar itself. Is there a way to generate a jar from specific packages in a project that's setup to package the whole project as a war?
I found information about the Maven jar plugin, but it says, "If the packaging of your project is set to 'jar', this plugin is executed whenever it passes the "package" phase." The packaging in my project is not set to jar. Is there still some way to use this plugin?
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
<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>
<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>
<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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With