Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Maven Build - Jar & War Files Using POM

 
Ranch Hand
Posts: 438
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We have to build a war and a jar from the same source codebase. The structure of the codebase is :

web/src/com/.... - contains java files
web/jsp - contains jsp files
web/css - contains css files
web/js - contains js files

we have to use maven for building the code. I understand the jar file can be created using exclude files. But, how to create the war file containing all the files. Using POM, we can create only single artifact.

Also, we have multiple projects say 10 projects which will generate 10 jars and a single war containing all jars with all jsps, css, js files.


How to achieve this using maven build?
 
Marshal
Posts: 3460
493
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know the details of what you are doing, but maybe you need two projects - one for the jar, and another for the war, where the war would have the jar as a dependency.
 
Vaibhav Gargs
Ranch Hand
Posts: 438
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ron, We need to build jar as well as war from same source code. Jar file is used to run on Unix as a java process - some schedularsetc. without any UI. and war is used to run as a web application having UI and backend.

Source code is in folder structure as:

proj/web/java
proj/web/jsp
proj/web/css
proj/web/js

So, jar should contain only proj/web/java class files and war shouldcontain all the files. And, i have given only one example folder we have around 30 such folders. SO, how to create 30 jars for classes and one war containing all java classes and jsp, css etc.
 
Saloon Keeper
Posts: 12826
279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ron has the right of it, put the classes in one project that builds a JAR, and put the web pages in another project that builds a WAR. The project that builds the WAR should have a dependency on the project that builds the JAR. Then, if you really want, you can configure your WAR project to include all its dependencies in the final artifact.
 
Saloon Keeper
Posts: 23441
159
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Build a JAR. Catalog it in your Maven repository.

2. Build a WAR, where the above JAR will be copied into the WAR's /WEB-INF/lib directory. There is no benefit to compiling the JAR classes directly into the /WEB-INF/classes directory. The net effect is the same either way and including the JAR is a lot less messy.

This approach means that you need 2 separate Maven projects, each with theit own POM. And, of course, if you're using an IDE, those 2 Maven projects should appear as separate IDE projects.

Now we get into logistical issues - that is, how tightly development of the JAR is bound to the WAR. You have 2 basic options. One is to define direct dependency of the WAR POM upon the JAR POM so that changes on the JAR will automatically trigger rebuilding of the WAR. The other option is for looser coupling. That is, the WAR POM demands a specific version of the JAR and has to explicitly requirest it, with that version having been built earlier. The second approach would be more appropriate for a single JAR used by multiple other projects, or in cases where it's more important to have a guaranteed-good release of the WAR than the latest build.

Externally, you could also implement build dependencies via a build system such as Jenkins. If different people will be working on the JAR and the WAR, you'd also want a shared local Maven repository such as Nexus.
 
Story like this gets better after being told a few times. Or maybe it's just a tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic