• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Generating a war file with Maven  RSS feed

 
Ranch Hand
Posts: 1329
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using Spring Boot with Maven, in the pom.xml by "default" I get

I need to generate a war, when I change it from pom to war I get the next error in the POM <parent> tag.



Any idea, please?

 
Saloon Keeper
Posts: 10218
216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well... the point of Spring Boot is to create a standalone application, not a WAR. If you want a WAR, then why are you using Spring Boot?

Anyway, the following guide might help you: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#build-tool-plugins-maven-packaging
 
Angus Ferguson
Ranch Hand
Posts: 1329
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I am building a standalone application but still I would need to generate a WAR. It was a need that was not contemplated at the beginning.

I have tried all possible Google solutions but still my target folder is empty.

Any idea or workaroud to build a .WAR?

 
Stephan van Hulst
Saloon Keeper
Posts: 10218
216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, what "default" are you talking about that had a POM packaging? As far as I'm aware, the result of a Spring Boot project is a JAR package.

And as ALWAYS, please post your POM when asking Maven questions.
 
Angus Ferguson
Ranch Hand
Posts: 1329
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By default teh packaging is pom, I cant change it to war because the parent tag gets an error if I do it.




</parent>[/code]

I cant post the whole POM but this piece is the scenario of this question.

 
Stephan van Hulst
Saloon Keeper
Posts: 10218
216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, what do you mean "by default"? Did you use an archetype to generate your project POM?

The snippet you posted looks like it doesn't belong to your project, but to Spring Boot.
 
Angus Ferguson
Ranch Hand
Posts: 1329
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used spring initializr then I imported it.

The main dependencies  I have are;
 
Bartender
Posts: 20736
124
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"pom" is not a standard Java package type like JAR, WAR, EAR, CAR, or the like. I don't know what that thing is, but it looks messed up.

The common use of Spring Boot is to create an executable JAR file that contains the Spring Boot bootstrap code, an embedded webapp server (Tomcat), and a webapp (WAR). You actually could probably extract the WAR it builds straight from the intermediate products assembled in the target directory.

Practically speaking, however, if you need both a deployable WAR and a stand-alone Spring Boot implementation of a webapp, you should have two projects, each with its own POM. The first project would build the WAR and the second project would have the first project as a dependency so that it could build the Spring Boot app and incorporate the WAR that was built from the first project.
 
Angus Ferguson
Ranch Hand
Posts: 1329
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now it looks like this POM without errors. The packaging WAR is there but the target folder contains the .class files but not the .war

 
Tim Holloway
Bartender
Posts: 20736
124
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A normal (not Spring Boot) Maven package operation - for example "mvn compile war:war" will result in two copies of the WAR in the target directory. One is the exploded (unzipped) copy of the WAR, which is what the various Maven goals have assembled. The other is the actual .war file itself, which the Maven war goal creates by simply zipping up all the files in the target's exploded WAR.

A Spring Boot POM should not be creating a WAR, since a WAR cannot be executed stand-alone the way a Spring Boot app is. Instead, as I said, you need a separate POM that can build a proper Spring Boot jar that incorporates the WAR you built. The WAR and its POM should not contain any Spring Boot code or references. Spring boot is not a webapp component, it's a component with a webapp inside it.
 
Angus Ferguson
Ranch Hand
Posts: 1329
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

you need a separate POM that can build a proper Spring Boot jar that incorporates the WAR you built.



Is there any small example out there, please? And how to integrate both applications?
 
Angus Ferguson
Ranch Hand
Posts: 1329
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This one has any reference tp Spring boot...This one should call the other one?


 
Angus Ferguson
Ranch Hand
Posts: 1329
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am following the next post

https://stackoverflow.com/questions/45351672/maven-war-packaging-with-value-war-is-invalid-aggregator-projects-require


Should the .war project be a new project in the same workspace?

 
Angus Ferguson
Ranch Hand
Posts: 1329
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With this POM module I get nothing, but when I remove the packaging .war then I get a jar. What could I do to get  a war, please?

 
Tim Holloway
Bartender
Posts: 20736
124
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I chased your links. You should study this one: https://books.sonatype.com/mvnex-book/reference/multimodule.html

Note that for this example product there are three projects:

1. A WAR project (simple-webapp)
2. A JAR project (simple-weather)
3. The parent project (simple-parent)

The WAR and JAR projects specify simple-parent to be their parent project. The simple-parent project specifies both the webapp and weather jar to be modules.

Note also that since the simple-weather jar is a library used in the simple-webapp, it is a dependency in the simple-webapp POM.

Taken all together, this makes it easy to build the product (webapp) either by running Maven separately on each child project (in the correct sequence) or by running Maven on the parent project.

Now here's one critical difference between the example and you. In the example, the war includes a jar. But in your project you are building a JAR that includes a war. So the dependency structure has to be adjusted accordingly.
 
Angus Ferguson
Ranch Hand
Posts: 1329
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The thing is that when I include the war module into the main app I want to get back a war of the main app. Does it makes sense?
 
Stephan van Hulst
Saloon Keeper
Posts: 10218
216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No.
 
Angus Ferguson
Ranch Hand
Posts: 1329
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My intention is to get  war of a webservice I have written...any idea, please?
 
Marshal
Posts: 5988
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Isaac Ferguson wrote:Yes I am building a standalone application but still I would need to generate a WAR. It was a need that was not contemplated at the beginning.


I don't think you've explained why you need a war file and a Spring Boot project, but if you do, you need two different pom files, as has been said earlier.
 
Stephan van Hulst
Saloon Keeper
Posts: 10218
216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have your dependencies in the wrong order. You need to create a WAR first, and then use Spring Boot to create a stand-alone application from that WAR.
 
Knute Snortum
Marshal
Posts: 5988
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Isaac Ferguson wrote:My intention is to get  war of a webservice I have written...any idea, please?


Then why do you need Spring Boot?  And if you don't, then create a pom file with <packaging>war</packaging>
 
Angus Ferguson
Ranch Hand
Posts: 1329
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because it is by default a parent and the packaging is pom
 
Stephan van Hulst
Saloon Keeper
Posts: 10218
216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No it's not. I just created a new empty project with Spring Initializr and it creates a project containing a POM which doesn't declare the <packaging> element, which means it's a JAR by default.
 
Angus Ferguson
Ranch Hand
Posts: 1329
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I remove the jar I get this error;
 
Stephan van Hulst
Saloon Keeper
Posts: 10218
216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably because you have a <modules> element in the POM.

You need a parent aggregator project with packaging POM, which has as sub-projects (or modules) one project for the WAR, and another project for the JAR. The JAR project has a dependency on the WAR.
 
Angus Ferguson
Ranch Hand
Posts: 1329
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes the parent aggregator has POM and then before </project>



But the module test 2 contains not he code of which I want to build the war-.... I need the war of the main app
 
Angus Ferguson
Ranch Hand
Posts: 1329
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need the war of the code associated with the parent POM.xml
 
Knute Snortum
Marshal
Posts: 5988
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Parent POMs only have packaging of "pom" (I believe).  That's why you need two child POMs: one that has jar packaging and one that has war.
 
Stephan van Hulst
Saloon Keeper
Posts: 10218
216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you ever gone through the Maven documentation? I feel that you lack understanding about some basic Maven concepts, and you would do well to review their reference material.
 
Tim Holloway
Bartender
Posts: 20736
124
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Have you ever gone through the Maven documentation? I feel that you lack understanding about some basic Maven concepts, and you would do well to review their reference material.



Seconded. In fact, I think you are under-educated on exactly what a webapp really is, what a WAR is, how a webapp server works, how Maven operates, and what's in Spring Boot.

I've said it before and I'll say it again.I know the CEO says "Computers aren't that hard! My little nephew Ralphie wrote a program just the other day. It makes 2 dots chase each other around on the screen. We don't need to spend lots of money on fancy trained programmers!" A Boy Scout can bandage a wound, but you don't want him (or her, since they've changed) to transplant your liver. There's a significant difference between basic competence and highly-trained personnel and only a fool doesn't recognize that you get what you pay for. OK, end of rant.

Seriously, we applaud your industriousness and we're doing all we can do to help, but you have so many holes in your understanding that a lot of times, we don't even understand the questions.

What I recommend is that you find some good training materials (or courses) to bring you up to basic understanding in the items I just mentioned. Ideally in the order I gave, since the later ones really require competence in the earlier ones.

In the event that you have an employer pushing you for specific results ASAP, well, you're probably doomed. About the best you could hope for is to create some sort of Frankenstein's Monster until you have a more solid foundation. And ideally, you could go to managment, get re-assigned to less taxing duties and they would sign you up for training. Of course, this is the 21st Century where it's more likely that they expect you to "hit the ground running", training isn't provided, and good luck on support. And by the way, you need to fix those LAN problems and repair the production database before you leave today, OK?

Regardless, keep at it. Your sheer unwillingness to give up means that it's likely that you too, might someday be looked to as a guru on the JavaRanch.
 
Angus Ferguson
Ranch Hand
Posts: 1329
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is just a small web app I am doing as a hobby, with a new tech stack for me (Spring Boot, Maven and so on)...

I am not using this forum for "professional" questions just for speed up a little bit with the basic stuff on this tech stack, it looks like it is not a way to go
 
Tim Holloway
Bartender
Posts: 20736
124
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Angus Ferguson wrote:It is just a small web app I am doing as a hobby, with a new tech stack for me (Spring Boot, Maven and so on)...

I am not using this forum for "professional" questions just for speed up a little bit with the basic stuff on this tech stack, it looks like it is not a way to go



OK, we're still here to help. But you are playing with industrial-grade tools and materials, so if you actually want to understand what you're doing instead of just stumbling around, you owe it to yourself to fill the gaps in your education. We can fill the small ones, but the best way to fill the larger ones is with some good external help. O'Reilly, Manning, and Apress would be happy to oblige you. Also, Oracle has online tutorials. And there are some good blogs out there, although I don't have any to recommend. I suppose a simple Google query "What is a WAR" would offer some starting points. Although the problem with learning from people's blogs is that some of those people have their own misconceptions.   One advantage of formally-published books and vendor-supplied guides is that they've usually been peer-reviewed before publishing. I was one of the reviewers for Pearson back several years ago, in fact.
 
Angus Ferguson
Ranch Hand
Posts: 1329
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This was the code missing in the child pom.xml. The .war is generated correctly now.


 
Knute Snortum
Marshal
Posts: 5988
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for posting the solution.
 
Tim Holloway
Bartender
Posts: 20736
124
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Congratulations!

Here's the official Maven docs on that: https://maven.apache.org/plugins/maven-war-plugin/faq.html#attached

Note that they, too recommend that you actually build the JAR in a separate POM, but this is for when you can't be bothered to do that.
 
Angus Ferguson
Ranch Hand
Posts: 1329
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is important to add the line in bold "failOnMissingWebXml" in order to avoid that the .war would be generated but empty.

Staff note (Knute Snortum):

BB codes, like [b], don't work inside [code] tags.

 
Knute Snortum
Marshal
Posts: 5988
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would think a better solution would be to setup a web.xml file.
 
Angus Ferguson
Ranch Hand
Posts: 1329
3
Java Netbeans IDE Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have concluded after "finishing" a small Spring Boot app which must generate a .war that the parent should contain all the logic and Java classes
then the child Spring Boot project containning the pom with the <packaging> war tag can be empty fo classes and logic.
 
Stephan van Hulst
Saloon Keeper
Posts: 10218
216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your conclusion is wrong. That project setup makes little sense. Parent projects are usually aggregators and can not contain logic.

As you've been told multiple times, you need to create a normal web application project that packages its artifacts as a WAR, and create a separate Spring Boot project that has a dependency on the first web application project. Then, you can OPTIONALLY aggregate them under a parent project.
 
Angus Ferguson
Ranch Hand
Posts: 1329
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay now it is correct.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!