Can I use Gradle to build a classpath string - gradle

I need to execute an external Java application as part of a Gradle build process. The external application needs the same JARs in its classpath as the application Gradle is building. Is there any way to expose the dependencies I have defined in Gradle to the external application?
i.e. I need to run this:
java -jar -cp [jars from Gradle] myapp.jar

You could add a simple task to your build to get the resulting classpath.
task printClasspath << {
println configurations.runtime.asPath
}

Related

how to call a gradle task which inside a jar with a parameter

I want to run a gradle task embedded in a jar, the jar which I am not producing myself. Is this even possible? I could also paste in the gradle task into my local build.gradle and run it but all the code would still need to reference the jar to actually run the main class.
You can add the jar which contains the Task you want to use in your buildscript classpath, using buildscript block. This way, you will be able to import and use this Task class in your build script. The buildscript block is generally used to add external Gradle Plugin jars into the script classpath, so that they can be applied, but you can also reference jars that provide no plugin implementation but only Task implementation classes.
Example
Assuming that:
the coordinates of the external jar is org.company.gradle:custom-tasks:0.1
the Task implementation class is org.company.gradle.tasks.MyTask
the Task implementation has a configurable property message
Then you can implement your build script as follows:
buildscript {
repositories {
// define repositories
}
dependencies {
// make the external jar available in the build script classpath
classpath "org.company.gradle:custom-tasks:0.1"
}
}
// use the Task
task 'myTask'(type: org.company.gradle.tasks.MyTask) {
message = "custom message"
}

Build Gradle JAR without dependencies

From command line, I need to build an executable jar without dependencies.
The current "gradle build" command gives me a jar with dependencies.
Couldn't find this on StackOverflow. If it's a duplicate question, point me there. Thanks.
As you have SpringBoot plugin enabled, the default behavior is to build an executable jar (fat-jar) containing all dependencies, through thebootJar task.
You can still generate a single "standard" jar if you need, this is explained in the documentation : Spring Boot Gradle plugin
jar {
enabled = true
}
bootJar {
classifier = 'boot'
}

How to change the port of a Spring Boot application using Gradle?

The simple question is: How can you change the Spring Boot application port with gradle?
Here are already listed a lot of correct answers if you are not using gradle. So for none gradle issues, please refere to this post.
In case you don't want to add extra configuration to your Gradle scripts, you can achieve it by setting the SERVER_PORT environment variable:
SERVER_PORT=8888 ./gradlew bootRun
[UPDATE] Since Gradle 4.9, it's possible to pass arguments to bootRun without extra configuration:
./gradlew bootRun --args='--server.port=8888'
If you're not already using the Spring Boot Gradle Plugin add it to your build script (of course, adapt the Spring Boot version to your needs):
buildscript{
ext { springBootVersion = '1.5.7.RELEASE' }
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'org.springframework.boot'
With this plugin you can do the following:
bootRun {
args += ["--server.port=[PORT]"]
}
obviously, replace [PORT] with the actual port number
OR for more dynamic you can use a project property to change the port. You have to do something similar like this:
if(!project.hasProperty("port"))
project.ext.set("port", 8080)
bootRun {
args += ["--server.port=${project.port}"]
}
Then you can start the application with
./gradlew bootRun -Pport=8888
If you skip the -Pport in this example it will use 8080.
Running by Gradle:
Run in default port(8080): ./gradlew bootRun
Run in provided port(8888): ./gradlew bootRun --args='--server.port=8888'
If we have any variable in the application.properties file named PORT, run this: PORT=8888 ./gradlew bootRun
Running by Maven:
Run in default port(8080): mvnw spring-boot:run
Run in provided port(8888): mvnw spring-boot:run -Dspring-boot.run.jvmArguments='-Dserver.port=8085'
Run in provided port(8888): mvn spring-boot:run -Dspring-boot.run.arguments='--server.port=8085'
Run in provided port(8888) with other custom property: mvn spring-boot:run -Dspring-boot.run.arguments="--server.port=8899 --your.custom.property=custom"
If we have any variable in the application.properties file named PORT, run this: SERVER_PORT=9093 mvn spring-boot:run
Using java -jar:
Create the .jar file:
For Gradle: ./gradlew clean build. We will find the jar file inside: build/libs/ folder.
For Maven: mvn clean install. We will find the jar file inside:target folder.
Run in default port(8080): java -jar myApplication. jar
Run in provided port(8888): java -jar myApplication.jar --port=8888
Run in provided port(8888): java -jar -Dserver.port=8888 myApplication.jar
Run in provided port(8888) having variable SERVER_PORT in application.properties file: SERVER_PORT=8888 java -jar target/myApplication.jar

How to make bootRepackage depends on jar not war when using Gradle War Plugin

Without Gradle War Plugin, bootRepackage task depends on jar task but with Gradle War Plugin, it depends on war task.
How can I change it to depend on jar task even though I'm using Gradle War Plugin?
UPDATE:
I'm using war task to create a war file including documents to be deployed to a documentation server and I want to use bootRepackaged jar file to provide a service. My war task depends on asciidoctor task which depends on test task (I'm using Spring REST Docs.) but I don't want to run asciidoctor task or test task when using bootRepackage task.
I solved my problem with the following setup:
ext {
mainClassName = 'com.izeye.throwaway.Application'
}
task myBootRepackage(type: BootRepackage, dependsOn: jar) {
}
but I'm not sure this is a good practice.
This is a sample project having the above configuration:
https://github.com/izeye/spring-boot-throwaway-branches/tree/war
You should have been able to do this:
bootRepackage {
withJarTask jar
}
While this correctly causes the jar task's jar to be repackaged, it doesn't remove the dependency on the war task. This is another symptom of this Spring Boot issue.
Until this issue has been resolved, the approach that you've taken – declaring your own BootRepackage task and manually configuring the tasks that it depends upon – is your best option.

Spring Boot: Launch other main method

I'm trying to launch another main method in another (test) jar.
According to this entry,I added
task packageTests(type: Jar) {
from sourceSets.test.output
}
to build.gradle to package all test resources and classes into a jar.
Then I executed
gradle bootRepackage which creates MyWS-0.0.1-SNAPSHOT.jar and
gradle packageTests which creates MyWS.jar.
Thereafter I copied both jars in a folder and created an application.properties with
loader.path=lib:/*,MyWS.jar
loader.main=db.DBTest
After reading the boot manual appendix, I try to launch the other main (in MyWS.jar/db.DBTest) with
java -cp ./MyWS-0.0.1-SNAPSHOT.jar;./MyWS.jar org.springframework.boot.loader.PropertiesLauncher -Dloader.main=db.DBTest
but the 'old' Start-Class of MyWS-0.0.1-SNAPSHOTs MANIFEST is still launched.
Any ideas how to solve this?

Resources