Spring Boot with Groovy and External Libraries - spring

I'm working on building what is effectively a throwaway Spring Boot application. Using the CLI, I can get a basic page up and working (see https://spring.io/guides/gs/spring-boot/). What I haven't figured out how to do, however, is how I can add external dependencies (ie. third-party JAR files) to the compile or runtime classpath when I use either the "spring run" or "spring jar" commands. Note that these external dependencies are local to my computer and are not stored in an artifact repository. Is there a simple way to do this?

If the jars aren't in an artifact repository, the easiest way to add them to the classpath is to use -cp when running your app or creating its jar.
For example:
spring run -cp foo.jar app.groovy
Or:
spring jar -cp foo.jar app.jar app.groovy
In the spring jar case, anything that's added to the classpath using -cp will be packaged inside the resulting jar (app.jar in this case) ensuring that it's self-contained.

you can use groovy's #Grab notation (there is also spring grab call). E.g.
#Grab('joda-time:joda-time:2.5')
#RestController
class ThisWillActuallyRun {
#RequestMapping("/")
String home() {
return new org.joda.time.DateTime().toString()
}
}

Related

Difference between Spring boot 2.5.0 generated *jar and *-plain.jar?

I upgraded my spring boot app to 2.5.0, then is app.jar and app-plain.jar is created by gradle.
I would like to the difference between these jars.
thanks
app.jar is the archive produced by the bootJar task. This is a Spring Boot fat jar that contains all of the module's dependencies as well as its classes and resources. It can be run using java -jar.
app-plain.jar is the archive produced by the jar task. This is a plain or standard jar file that contains only the module's classes and resources.
You can learn a bit more about this in the documentation for Spring Boot's Gradle plugin.

How to Run the spring boot jar

I have spring boot jar. It contains boot-inf folder with that folder it contains classes and lib folder. I need to run the certain class which is having main method. But I don't know how to run it. For normal jar we can use the below format to run the class from the jar.
java -cp "sample.jar;dependecy.jar" com.sample.ClassName
But in spring boot jar what is the format to run the class. Because it contains boot-inf folder. I am using gradle script to build the project
It should be very easy, just:
java -jar sample.jar
where sample.jar is your spring boot jar.
See https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-running-your-application.html
You can try with the distribution to run the class in a jar. Build the project to create the dist. Then you can run the class as you mentioned in the question.
java -cp "sample.jar;dependecy.jar" com.sample.ClassName

Spring boot application as a shipable standalone jar

So I created a spring boot application. And I simply like to run it as a program from a Main class. No need for web controller access.
It runs great in my Intellij.
But how do I ship it as a jar?
It depends on your project structure. If you use maven just run "maven install" and find your jar in your local repository.
When you have it, run "java -jar your.jar"

How to generate executable scripts using spring boot maven plugin

Using spring boot maven plugin we are able to generate executable jars. And we can execute the jar using java -jar ...
In spring boot there is another option for installation . This generates the jar which can be added in init.d.
But is it possible to generate a sh|cmd file which can be used to start|stop|restart spring boot applications?
The executable true flag to create a 'fully executable’ jar actually pre-pends a shell script into the beginning of the jar.
It works outside init.d too. Try this:
./myapp.jar start

spring boot System.getProperty("java.class.path") doesn't include lib/ jars

I've got a spring boot app and I'm building a myApp.jar using the spring-boot antlib. When I jar -tf myApp.jar I see that I have a jar called lib/foo.jar. Yet when I print out System.getProperty("java.class.path") I don't see that jar file on the classpath. I also get a ClassNotFound exception from URLClassLoader when the code attempts to use this class for the first time. I'm using the JarLauncher since that's what the antlib defaults to.
Any ideas why this jar file would not be on the classpath?
You won't see a bundled JAR in System.getProperty('java.class.path'). The class path specifies where the JVM will look in the filesystem for classes you attempt to load.
Spring Boot uses fat JARs, which are loaded in a completely different way. Refer to the Spring Boot documentation.

Resources