Gradle Spring Boot: Add folder at root to jar - spring-boot

So I have a folder .ebextensions at the root of my spring boot project,and those files are not being included in my jar when I use "bootpackage" in my gradle plugin for intellij.I am deploying the jar On Amazon Web Services
How do I include these files in my jar?
My build.gradle
buildscript {
ext {
springBootVersion = '1.5.9.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
group = 'haughton.daniel'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
ext {
springCloudVersion = 'Edgware.SR1'
}
dependencies {
//compile 'org.springframework.cloud:spring-cloud-starter-aws'
compile('org.springframework.boot:spring-boot-starter-data-jpa')
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail
compile group: 'org.springframework.boot', name: 'spring-boot-starter-mail', version: '2.0.0.RELEASE'
compile('org.springframework.boot:spring-boot-starter-jdbc')
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
// https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity4
compile group: 'org.thymeleaf.extras', name: 'thymeleaf-extras-springsecurity4', version: '2.1.2.RELEASE'
// https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-aws-autoconfigure
//compile group: 'org.springframework.cloud', name: 'spring-cloud-aws-autoconfigure', version: '1.2.2.RELEASE'
// https://mvnrepository.com/artifact/mysql/mysql-connector-java
// https://mvnrepository.com/artifact/mysql/mysql-connector-java
compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.6'
compile('org.springframework.boot:spring-boot-starter-web')
compile ('org.apache.tomcat:tomcat-dbcp:8.0.30')
runtime('mysql:mysql-connector-java')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.security:spring-security-test')
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}

Solution:
Added this to my build.gradle
processResources {
from ('.ebextensions/') {
into '.ebextensions'
}
}

I added an ebextensions folder into source of my project, this contains the .ebextensions folder with nginx config:
Folder structure
I extended bootJar task:
bootJar {
baseName = 'project'
version = '1.0.0'
from('ebextensions')
//...
When I run ./gradlew cle ass bootJar, then the unzipped project-1.0.0.jar contains:
.ebextensions
BOOT-INF
META-INF
org
I used Gradle 4.10.
The root cause was 413 Request Entity Too Large with ElasticBeanstalk.
Full content of proxy.conf:
client_max_body_size 20M;

Related

How to resolve invalid packaging for parent POM, must be "pom" but is "jar"

To resolve spring-framework vulnerability posted by spring.io
I tried upgrading spring-boot version from 2.4.5 to 2.5.12 and with gradle-6.8 version, on running ./gradlew clean build task is failing with error
Invalid packaging for parent POM org.apache.logging.log4j:log4j-api:2.17.2, must be "pom" but is "jar" in org.apache.logging.log4j:log4j-api:2.17.2
The dependency org.springframework.boot:spring-boot-starter-webflux loads the internal dependency log4j-api:2.17.2
How to resolve invalid parent POM packaging for internal dependencies?
build.gradle
buildscript {
ext {
springBootVersion = '2.5.12'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
{
exclude group: 'org.slf4j', module: 'slf4j-ext'
}
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.service'
version = ''
sourceCompatibility = 11
def logbackVersion = '1.2.3'
repositories {
mavenCentral()
}
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
if (details.requested.group == 'org.apache.logging.log4j') {
details.useVersion '2.17.1'
}
}
}
dependencies {
implementation ('org.springframework.boot:spring-boot-starter-webflux')
developmentOnly('org.springframework.boot:spring-boot-devtools')
testImplementation('org.springframework.boot:spring-boot-starter-test')
testImplementation('io.projectreactor:reactor-test')
implementation("ch.qos.logback:logback-core:${logbackVersion}")
implementation("ch.qos.logback:logback-classic:${logbackVersion}")
implementation('org.apache.httpcomponents:httpclient:4.5.11')
implementation('org.apache.commons:commons-collections4:4.4')
implementation("org.springframework.cloud:spring-cloud-vault-config:2.1.3.RELEASE")
implementation("org.springframework.cloud:spring-cloud-vault-config-consul:2.1.3.RELEASE")
implementation group: 'org.springframework.cloud', name: 'spring-cloud-consul-dependencies', version: '1.0.0.RELEASE', ext: 'pom'
implementation('com.amazonaws:aws-java-sdk-sqs:1.11.634')
implementation('org.projectlombok:lombok:1.18.12')
implementation('org.yaml:snakeyaml:1.26')
implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
annotationProcessor('org.projectlombok:lombok:1.18.12')
implementation group: 'org.bouncycastle', name: 'bc-fips', version: '1.0.2'
implementation group: 'org.bouncycastle', name: 'bctls-fips', version: '1.0.11'
}
Adding mavenBom spring-cloud-dependencies helped resolve this issue. Suspecting webflux pulled in a transitive dependency to an older release and adding spring-cloud-dependencies bom in dependencyManagement ensured all Spring dependencies are at the same version
Here's update build.gradle file that worked
plugins {
id 'org.springframework.boot' version '2.6.6'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'application'
}
group = 'com.service'
version = '1.0.0-SNAPSHOT'
sourceCompatibility = '11'
application {
mainClass = 'com.service.scheduler.SchedulerApplication'
}
repositories {
mavenCentral()
}
ext {
set('springCloudVersion', "2021.0.1")
set('logbackVersion', "1.2.11")
}
bootJar {
archiveFileName = 'scheduler.jar'
}
bootRun {
systemProperties = System.properties
}
dependencies {
/*---spring dependencies---*/
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'org.springframework.cloud:spring-cloud-starter-vault-config'
implementation 'org.springframework.cloud:spring-cloud-vault-config-consul'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.projectreactor:reactor-test'
implementation 'com.amazonaws:aws-java-sdk-sqs:1.12.187'
implementation 'org.apache.httpcomponents:httpclient:4.5.13'
implementation 'org.apache.commons:commons-collections4:4.4'
implementation 'org.yaml:snakeyaml:1.30'
implementation 'com.google.code.gson:gson:2.9.0'
/*---fips dependencies---*/
implementation group: 'org.bouncycastle', name: 'bc-fips', version: '1.0.2'
implementation group: 'org.bouncycastle', name: 'bctls-fips', version: '1.0.11'
/*---lombok dependencies---*/
implementation 'org.projectlombok:lombok:1.18.22'
annotationProcessor 'org.projectlombok:lombok:1.18.22'
/*---logback dependencies---*/
implementation("ch.qos.logback:logback-core:${logbackVersion}")
implementation("ch.qos.logback:logback-classic:${logbackVersion}")
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}

Creating Jar for SpringBoot Application with JSP pages using Gradle

I have an SpringBoot Application which has few JSP pages within it. When I boot the main class from my eclipse it is working perfectly. But at the same time, when I package it as jar, the WEB-INF/jsp folder is not configured properly. I am stuck here. Request your help
Below is my Gradle script
buildscript {
ext {
springBootVersion = '1.5.4.RELEASE'
}
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
group = 'com.arun'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
task packSSO(type: Jar) {
manifest {
attributes(
'Implementation-Title': 'Arun Spring Boot Application',
'Implementation-Version': version,
'Built-By': System.getProperty('user.name'),
'Built-Date': new Date(),
'Main-Class': 'com.arun.MainGate',
'Built-JDK': System.getProperty('java.version')
)
}
sourceSets {
main {
resources {
srcDirs "src/main/resources"
}
}
}
baseName = project.name + '-all'
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
}
with jar
}
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-tomcat')
compile('org.apache.tomcat.embed:tomcat-embed-jasper')
compile ('javax.servlet:jstl:1.2')
testCompile('org.springframework.boot:spring-boot-starter-test')
testImplementation 'junit:junit:4.12'
}
I exploded the created Jar and the structure looks like below.
so when i run java -jar SSOPage-0.0.1-SNAPSHOT.jar it couldn't find the JSP pages. What exact folder structure should i need to follow and how to package the WEB-INF in gradle ?
I think you must use WAR packaging for a Spring Boot application that uses JSPs. However, you can still use it like a self-contained executable JAR file, as in $ java -jar ./build/libs/hcro-pdi-1.0.0.war.
Here's my build.gradle file from a Spring Boot 2.0.1 project that uses JSPs.
plugins {
id 'org.springframework.boot' version '2.0.1.RELEASE'
id "io.spring.dependency-management" version "1.0.4.RELEASE"
}
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
bootWar {
baseName = 'hcro-pdi'
version = '1.0.0'
}
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
}
}
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
compile('org.springframework.boot:spring-boot-starter-jdbc')
compile('javax.servlet:jstl')
compile('org.apache.tomcat.embed:tomcat-embed-jasper')
// Devtools enable hot-reloading of JSP and static content
compile("org.springframework.boot:spring-boot-devtools")
compile(group: 'org.mariadb.jdbc', name: 'mariadb-java-client', version: '2.2.0')
compile(group: 'org.apache.commons', name: 'commons-lang3', version: '3.7')
compile(group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.5')
compile(group: 'joda-time', name: 'joda-time')
compile(group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-joda')
compileOnly('org.projectlombok:lombok')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
// When running in bootRun task, automatically reload static resources when changed
// https://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-gradle-plugin.html#build-tool-plugins-gradle-running-applications
bootRun {
jvmArgs "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005"
sourceResources sourceSets.main
}
And, you do have to add the two properties to application.properties shared by #Yogi.
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
You need to create application.properties under resources folder in your application and define below properties:
spring.mvc.view.prefix: /WEB-INF/jsp/ (your path for JSP files)
spring.mvc.view.suffix: .jsp
Sample MVC examples:
http://www.springboottutorial.com/creating-web-application-with-spring-boot

Slf4j with log4j2 not working Spring boot 1.4.3.RELEASE

I was using Spring boot 1.3.6.RELEASE and log4j2.yml as below:
Configuration:
status: info
Properties:
Property:
name: log-path
value: "/dvl-log/pol/apps/logs/api"
Appenders:
Console:
- name: Console
target: SYSTEM_OUT
PatternLayout:
Pattern: "%d{HH:mm:ss.SSS} %-5level %logger{1} - %msg%n"
Loggers:
Root:
level: info
AppenderRef:
- ref: Console
Logger:
- name: com.company.api
level: trace
additivity: false
AppenderRef:
- ref: Console
level: trace
The logging with below code was working perfectly.
#Slf4j
public class LogExample{
public void logMethods(String className,String methodName){
log.trace("{} >> {}", className, methodName);
}
}
But when I upgraded to Spring Boot 1.4.3.RELEASE, the logger is not working.
My new build.gradle after upgrading to 1.4.3 is (just a version change)
version '1.0'
// dependencies for command line
buildscript {
ext {
springBootVersion = '1.4.3.RELEASE'
dependencyManagementVersion = '0.5.2.RELEASE'
}
repositories {
jcenter()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
classpath "io.spring.gradle:dependency-management-plugin:${dependencyManagementVersion}"
}
}
apply plugin: "io.spring.dependency-management"
apply plugin: 'java'
apply plugin: 'spring-boot'
apply plugin: 'war'
apply plugin: 'eclipse'
apply plugin: 'idea'
// JDK 8
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
repositories {
jcenter()
}
ext {
springCloudVersion = 'Brixton.SR4'
springBootVersion = '1.4.3.RELEASE'
swaggerVersion = '2.4.0'
jodaTimeVersion = '2.9.4'
jacksonJodaVersion = '2.5.1'
junitVersion = '4.12'
springWsTestVersion = '2.2.3.RELEASE'
lombokVersion = '1.16.10'
jsonPathVersion = '2.2.0'
ehcacheVersion = '3.2.0'
javaxCacheVersion = '1.0.0'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
mavenBom "org.springframework.boot:spring-boot-starter-parent:${springBootVersion}"
}
}
sourceSets {
test {
java {
srcDir 'src/test/unit/java'
}
resources {
srcDir 'src/test/unit/resources'
}
}
}
dependencies {
/* core libraries */
compile('org.springframework.cloud:spring-cloud-starter-config') {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}
compile('org.springframework.boot:spring-boot-starter-web') {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}
compile("org.springframework.boot:spring-boot-starter-hateoas"){
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}
compile 'org.springframework.ws:spring-ws-core'
// logging
compile('org.springframework.boot:spring-boot-starter-log4j2')
compile('com.fasterxml.jackson.dataformat:jackson-dataformat-yaml')
compile('com.fasterxml.jackson.core:jackson-databind')
// embedded server
providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
// https://mvnrepository.com/artifact/org.projectlombok/lombok-maven
compile "org.projectlombok:lombok:${lombokVersion}"
// https://mvnrepository.com/artifact/com.jayway.jsonpath/json-path
// A Java DSL for reading JSON documents
compile "com.jayway.jsonpath:json-path:${jsonPathVersion}"
/* plugins */
/* test libraries */
// unit
testCompile "junit:junit:${junitVersion}"
testCompile "org.springframework.boot:spring-boot-starter-test"
testCompile "org.springframework.ws:spring-ws-test:${springWsTestVersion}"
}
war {
archiveName = "${project.name}.war"
}
What thing I'm missing here? TIA
I want to add more details:
there are log4j-api:2.6.2, log4j-core:2.6.2 and log4j-slf4j-impl:2.2.6 on classpath after upgrading to Spring Boot 1.4.3. The thing is appender is not working for trace level because I am getting log for info or above level.
I resolved this problem by moving log4j2.yml directly into resource directory.
Earlier, with Spring Boot 1.3.6, I had put log4j2.yml under resources/logging directory with configuration.yml as
logging:
config: classpath:logging/log4j2.yml
I don't know why it didn't work for 1.4.3.RELEASE.

Gradle - Exclude provided scope jars from War file

Gradle seems to claim that it doesnt include the providedCompile and providedRuntime scopes when building the war file.
But, when i do the build with below war config, a folder named "lib-provided" seems to be containing all the provided scoped jars. How do I limit this functionality to NOT include the provided scoped jars.
configure(rootProject) {
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'java'
targetCompatibility = 1.8
sourceCompatibility = 1.8
repositories {
mavenCentral()
mavenLocal()
maven { url "https://repository.jboss.org/nexus/content/groups/public-jboss/" }
}
// Import Spring Boot's bom, spring-boot-dependencies
dependencyManagement {
imports {
mavenBom 'org.springframework.boot:spring-boot-dependencies:1.2.5.RELEASE'
}
}
// Override the spring-data-releasetrain.version property
ext['spring-data-releasetrain.version'] = 'Fowler-SR1'
dependencies {
compile("org.springframework.boot:spring-boot-starter-actuator")
compile("org.springframework.boot:spring-boot-starter-security")
............ Other Spring Boot based projects
testCompile("org.springframework.boot:spring-boot-starter-test")
compile("com.fasterxml.jackson.datatype:jackson-datatype-hibernate4:2.4.6")
......... Below are the "Provided" Scoped packages
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
providedCompile("org.apache.tomcat.embed:tomcat-embed-jasper:8.0.23")
providedCompile("org.hibernate:hibernate-core:4.3.7.Final")
providedCompile("org.hibernate:hibernate-infinispan:4.3.7.Final")
providedCompile("org.hibernate:hibernate-entitymanager:4.3.7.Final")
providedCompile("org.hibernate:hibernate-validator:5.1.3.Final")
providedCompile("org.hibernate:hibernate-search-orm:5.0.1.Final")
providedCompile("org.hibernate.common:hibernate-commons-annotations:4.0.4.Final")
providedCompile("org.infinispan:infinispan-core:7.1.1.Final")
providedCompile("org.infinispan:infinispan-query:7.1.1.Final")
testCompile("com.microsoft.sqlserver:sqljdbc41:4.1")
}
configurations {
providedCompile
// replaced with jcl-over-slf4j
all*.exclude group: 'commons-logging', module: 'commons-logging'
// replaced with log4j-over-slf4j
all*.exclude group: 'log4j', module: 'log4j'
}
}
war {
baseName = 'abc'
version = '5.0.0-SNAPSHOT-' + + System.currentTimeMillis();
doFirst {
manifest {
attributes("Implementation-Title": project.name, "Implementation-Version": version, "Implementation-Timestamp": new Date())
}
}
webAppDirName = 'web'
includeEmptyDirs false
archiveName 'abc.war'
}
Thanks!
In the below configuration, Gradle 4.6
None of the below depndnecies will be in the War/WEB-INF lib.
If you have an Ear build.gradle can contain entry like below , which will ensure all depndence jar in the Ear/lib folder.
if the below entry is made, The generated Ear file will have .war file and the War/lib folder will not be having the specified dependent files.
apply plugin: 'war'
earlib project(path: ':MyWebProject', configuration: 'compile')
deploy project(path: ':MyWebProject', configuration: 'archives')
apply plugin: 'war'
dependencies {
providedCompile('org.apache.logging.log4j:log4j-web')
providedCompile('org.springframework.boot:spring-boot-starter-web') {
exclude module: "spring-boot-starter-tomcat"
}
}

How to exclude specific jars from WEB-INF/lib

I have the following gradle projects:
jar-module-A
+-- JavaEE lib(Dependency)
war-module-A
+-- jar-module-A
I want to exclude JavaEE lib from WEB-INF/lib.
Using providedCompile:
Since jar-module-A is not a web module but jar, I cannot use providedCompile in build.gradle of jar-module-A.
configurations { runtime.exclude JavaEE-lib }
It excludes JavaEE from not only runtime but also testRuntime, It fails my unit tests in war-module-A by ClassNotFoundException.
How can mitigate this situation?
Brute force solution would be:
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'maven'
repositories {
mavenCentral()
maven {
url "file:///tmp/repo"
}
}
uploadArchives {
repositories {
mavenDeployer {
repository(url: "file:///tmp/repo")
}
}
}
dependencies {
compile group: 'ruimo', name: 'module_a', version: '1.0-SNAPSHOT'
testCompile group: 'junit', name: 'junit', version: '4.10'
}
war {
classpath = classpath.filter {
it.name != 'javaee-api-6.0.jar'
}
}
for module_b. It might be filename dependent (not sure of that). Maybe will have a look later on, but not sure - short with time.
UPDATE
More sophisticated:
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'maven'
repositories {
mavenCentral()
maven {
url "file:///tmp/repo"
}
}
uploadArchives {
repositories {
mavenDeployer {
repository(url: "file:///tmp/repo")
}
}
}
dependencies {
compile(group: 'ruimo', name: 'module_a', version: '1.0-SNAPSHOT') {
transitive = false// here You can exclude particular dependency, not necessarily all
}
providedCompile group: 'javax', name: 'javaee-api', version: '6.0'
testCompile group: 'junit', name: 'junit', version: '4.10'
}
No I see it can be done with many ways. It depends on what are other requirements regard build.
If the transitive is not working, can try resolution startegy.
I was fed up with the transitive or even normal exclude.
https://docs.gradle.org/2.2.1/dsl/org.gradle.api.artifacts.ResolutionStrategy.html
dependencies{
compile(group: 'ruimo', name: 'module_a', version: '1.0-SNAPSHOT')
}
configurations.all {
resolutionStrategy {
force ruimo:module_a:1.0-SNAPSHOT
}
}

Resources