Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for creating layered war files with Gradle #22195

Closed
RodrigoPetter opened this issue Jul 1, 2020 · 7 comments
Closed

Add support for creating layered war files with Gradle #22195

RodrigoPetter opened this issue Jul 1, 2020 · 7 comments
Assignees
Labels
type: enhancement A general enhancement
Milestone

Comments

@RodrigoPetter
Copy link

Spring Boot 2.3 introduced a new feature where it is possible to create layered .jar files. This is very useful for docker images layer optimization as described in this blog post: https://spring.io/blog/2020/01/27/creating-docker-images-with-spring-boot-2-3-0-m1?#layered-jars

Unfortunately this doesn't work when generating a war file.

Take this build.gradle as an example:

plugins {
	id 'org.springframework.boot' version '2.3.1.RELEASE'
	id 'io.spring.dependency-management' version '1.0.9.RELEASE'
	id 'java'
	id 'war'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '14'

repositories {
	mavenCentral()
}

ext {
	set('springCloudVersion', "Hoxton.SR6")
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-web'
	testImplementation('org.springframework.boot:spring-boot-starter-test') {
		exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
	}
}

bootJar {
	layered()
}

dependencyManagement {
	imports {
		mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
	}
}

test {
	useJUnitPlatform()
}

As soon as I add the war plugin I start to receive the following error when trying to use the layertools:

$ java -Djarmode=layertools -jar build/libs/demo-0.0.1-SNAPSHOT.war list
Unsupported jarmode 'layertools'
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jul 1, 2020
@wilkinsona
Copy link
Member

Thanks for the suggestion. Why do you want to use a war file rather than a jar file to create a Docker image?

@wilkinsona wilkinsona added the status: waiting-for-feedback We need additional information before we can continue label Jul 2, 2020
@RodrigoPetter
Copy link
Author

Is there anything wrong with using a .war inside a docker image? Perhaps I am missing some specific knowledge.

Anyway, this is my use case:

I have a webapp with thymeleaf. The following file structure works without any aditional configuration when using the war plugin:

image

Until today, we alway execute ./gradle bootWar in ours pipeline, then we put the .war file inside a docker image with the following entrypoint: ENTRYPOINT ["java", "-jar", "/application.war"]

If necessary, I could always download the .war artifact from the pipeline and run it locally.

Since we work with microservice, setting up a development environment can be quite painful, so we use the .jar and .war artifacts from the pipelines.

I see two negative points if this layer feature only works with a .jar:

  1. The artifact from my pipeline will not work since it doesn't contain the files from the webapp folder.
  2. Additional configurations will be necessary to achieve the same behavior as the war plugin.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Jul 2, 2020
@wilkinsona
Copy link
Member

Thanks for the additional information. Generally speaking, the only reason to use a .war file is if you are deploying to a servlet container or you want to use JSPs. When neither of those is the case, we recommend using jar packaging.

From your screenshot above, it looks as if you could switch to .jar packaging by moving src/main/webapp/assets to src/main/resources/static/assets, src/main/webapp/favicon.ico to src/main/resources/static/favicon.ico and src/main/webapp/WEB-INF/views to src/main/resources/templates.

@RodrigoPetter
Copy link
Author

@wilkinsona You are completely right. I made these changes and everything worked as expected (with some adjustments to the thymeleaf settings).

I'm sorry for the inconvenience.

There is even documentation about it here: https://spring.io/guides/gs/serving-web-content/#_add_a_home_page

@mbhave mbhave added status: declined A suggestion or change that we don't feel we should currently apply and removed status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged labels Jul 2, 2020
@derTobsch
Copy link

Thanks for the additional information. Generally speaking, the only reason to use a .war file is if you are deploying to a servlet container or you want to use JSPs. When neither of those is the case, we recommend using jar packaging.

From your screenshot above, it looks as if you could switch to .jar packaging by moving src/main/webapp/assets to src/main/resources/static/assets, src/main/webapp/favicon.ico to src/main/resources/static/favicon.ico and src/main/webapp/WEB-INF/views to src/main/resources/templates.

Sorry for adding a comment into a already closed issue. We have a spring boot 2.3.2 application with jsps and so we have a war packaging. Is it possible to use the build-image goal? That would be a nice feature for us to reduce code and have the benefit of layered docker images until we get rid of the jsps.

@wilkinsona
Copy link
Member

Is it possible to use the build-image goal?

Not at the moment, no. Both layering and the build image goal are only supported with jar packaging.

@wilkinsona wilkinsona reopened this Aug 4, 2020
@wilkinsona wilkinsona added for: team-attention An issue we'd like other members of the team to review status: waiting-for-triage An issue we've not yet triaged and removed status: declined A suggestion or change that we don't feel we should currently apply labels Aug 4, 2020
@philwebb philwebb added type: enhancement A general enhancement and removed for: team-attention An issue we'd like other members of the team to review status: waiting-for-triage An issue we've not yet triaged labels Aug 5, 2020
@philwebb philwebb added this to the 2.4.x milestone Aug 5, 2020
@dodgex
Copy link

dodgex commented Aug 6, 2020

We are currently migration to Spring Boot 2.3.x and Docker for some of our projects and would love to use the layering to build our docker images.

But as some of our products are deployed to customer servers and some of them have Docker (yet?) we would need to build the project as jar and war. Having just a war that we can deploy in any tomcat and build a layerd Docker image from would be awesome.

@wilkinsona wilkinsona changed the title The new layered .jar feature should also work with .war Add support for creating layered war files Aug 6, 2020
@wilkinsona wilkinsona changed the title Add support for creating layered war files Add support for creating layered war files with Gradle Oct 19, 2020
@philwebb philwebb modified the milestones: 2.4.x, 2.5.x Oct 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

7 participants