Build thin Spring Boot jar

My springboot applications runs well if use maven pom to build but I want the incremental build so try to move to Gradle but can not startup now

my pom.xml

<build>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-dependency-plugin</artifactId>
			<executions>
				<execution>
					<id>copy-dependencies</id>
					<phase>package</phase>
					<goals>
						<goal>copy-dependencies</goal>
					</goals>
					<configuration>
						<outputDirectory>target/lib</outputDirectory>
						<excludeTransitive>false</excludeTransitive>
						<stripVersion>false</stripVersion>
						<includeScope>runtime</includeScope>
					</configuration>
				</execution>
			</executions>
		</plugin>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-jar-plugin</artifactId>
			<configuration>
				<excludes>
					<exclude>**/*.properties</exclude>
					<exclude>**/*.xml</exclude>
					<exclude>**/*.yml</exclude>
					<exclude>static/**</exclude>
					<exclude>templates/**</exclude>
				</excludes>
			</configuration>
		</plugin>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
			<configuration>
				<layout>ZIP</layout>
				<includes>
					<include>
						<groupId>non-exists</groupId>
						<artifactId>non-exists</artifactId>
					</include>
				</includes>
			</configuration>
			<executions>
				<execution>
					<goals>
						<goal>repackage</goal>
					</goals>
					<configuration>
						<attach>false</attach>
					</configuration>
				</execution>
			</executions>
		</plugin>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-antrun-plugin</artifactId>
			<executions>
				<execution>
					<phase>package</phase>
					<goals>
						<goal>run</goal>
					</goals>
					<configuration>
						<target>
							<property name="dist">target/${project.artifactId}</property>
							<property name="dist-tmp">target/${project.artifactId}/tmp</property>
							<property name="app-name">${project.artifactId}-${project.version}</property>
							<mkdir dir="${dist-tmp}" />
							<copy file="target/${app-name}.jar"
								tofile="${dist-tmp}/${app-name}.jar" />
							<unzip src="${dist-tmp}/${app-name}.jar"
								dest="${dist-tmp}" />
							<delete file="${dist-tmp}/${app-name}.jar" />
							<move file="target/${app-name}.jar" todir="${dist}" />
							<delete dir="${dist-tmp}" />
							<copy todir="${dist}">
								<fileset dir="target/classes">
									<include name="**/*.properties" />
									<include name="**/*.xml" />
									<include name="**/*.yml" />
									<include name="**/shell/*.sh" />
								</fileset>
							</copy>
						</target>
					</configuration>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>

My build.gradle

plugins {
  id "org.springframework.boot" version "2.0.0.RELEASE"
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'

group = 'com.xxx'
version = '1.0.0-SNAPSHOT'

description = "service-demand"

sourceCompatibility = 1.8
targetCompatibility = 1.8
tasks.withType(JavaCompile) {
	options.encoding = 'UTF-8'
}

tasks.withType(Jar) {
destinationDir = file("$rootDir/build")
}

repositories {
 maven { url "http://nexus/repository/maven-public" }
}

dependencies {
...
}

task copyLib(type: Copy) {
copy {
    from configurations.runtime
    into "$buildDir/lib"
}

doFirst {
    if(inputs.empty) throw new GradleException("Input source for copyLib doesn't exist")
}
}

And I got error when startup

Exception in thread “main” java.lang.NoClassDefFoundError: javax/servlet/MultipartConfigElement
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.getDeclaredMethod(Class.java:2128)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:47)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
Caused by: java.lang.ClassNotFoundException: javax.servlet.MultipartConfigElement
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:93)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

I compared the jar files, the only difference is Gradle build jar include mapper and properties files, so I tried remove mapper folder and reources files and got the same problem.

Nothing useful found in Google, so please help me, thanks.

I found the problem is bootJar task:

  1. change dependencies as compile then the jar file include all external jar files and runs well
  2. change dependencies as compileClasspath then the jar file exclude all external jar files but got class not found exception.

I want to exclude dependencies jar from bootJar task and also can load if I specify loader.path, how to do that?
Another way of saying, I want thin Spring Boot jar if use Gradle to build.


this plugin works well for build thin bootJar

Section 4.3 of the spring boot gradle plugin docs describes how to re-enable the creation of a thin jar.
https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/gradle-plugin/reference/html/

Yes, this works well for now. The thin launcher plugin should be improved in some situations.