Hey, this is moving to my own domain!!
http://www.marcelustrojahn.com
@TrojahnSnippets
2015-11-25
2015-11-23
Spring-Boot / Groovy / Gradle "Hello World" application
The complete code for this project can be found here:
https://github.com/mtrojahn/spring-boot-groovy-console-app
Last weekend I was playing around with Gradle which I have never used before. The result is this very basic spring-boot project just to illustrate how I did it. Gradle isn't that difficult but I had a few problems until I learn about the spring-boot-gradle-plugin and such.
This is the build.gradle:
group 'com.mtrojahn' version '1.0-SNAPSHOT' buildscript { repositories { mavenCentral() } dependencies { classpath "org.springframework.boot:spring-boot-gradle-plugin:1.3.0.RELEASE" } } apply plugin: 'groovy' apply plugin: 'spring-boot' repositories { mavenCentral() } sourceCompatibility = 1.8 targetCompatibility = 1.8 dependencies { compile "org.springframework.boot:spring-boot-starter" compile 'org.codehaus.groovy:groovy-all' testCompile "junit:junit" } bootRun { addResources = true }
As you can see it's fairly simple and way more organized than Maven in my opinion. The spring-boot-gradle-plugin is the key here. It provides all the version information for the dependencies and also adds a Gradle task "bootRun" which you can use to run your project.
Now we just need a main class:
@SpringBootApplication class Application { static main(args) { def context = new SpringApplicationBuilder() .sources(Application.class) .bannerMode(Banner.Mode.OFF) .run() def app = context.getBean(Application.class) app.start() } def start() { println("Hello World!") } }
Not that different from Java, right?
I'm a complete newbie on Gradle and Groovy but I really liked what I saw so far... If I made some mistake please leave a comment.
See ya!
2015-11-16
Spring-boot / Maven simple "Hello World" application
The complete code for this application:
https://github.com/mtrojahn/spring-boot-console-app
In this example I'll try to show how to create a simple console application to print out the traditional "Hello World" message using spring-boot on a Maven project. I'll try to keep it as simple as possible for now and I'll most likely use this code as base for new posts.
Let's start with the pom.xml file.
4.0.0 com.mtrojahn spring-boot-console-app 1.0-SNAPSHOT jar UTF-8 1.8 com.mtrojahn.boot.Application org.springframework.boot spring-boot-starter-parent 1.3.0.RELEASE org.springframework.boot spring-boot-starter org.apache.maven.plugins maven-compiler-plugin 3.3 1.8 org.apache.maven.plugins maven-surefire-plugin 2.19 true org.springframework.boot spring-boot-maven-plugin repackage
This is a simple console application so, for now, we only need "spring-boot-starter" as dependency.
You can also notice that I included a few plugins. The most important for now is the spring-boot-maven-plugin which will allow us to run the project with the "mvn spring-boot:run" Maven directive.
And now the main class:
@SpringBootApplication public class Application { public static void main(String[] args) { ConfigurableApplicationContext context = new SpringApplicationBuilder() .sources(Application.class) .bannerMode(Banner.Mode.OFF) .run(args); Application app = context.getBean(Application.class); app.start(); } private void start() { System.out.println("Hello World!"); } }
That's it.
The annotation @SpringBootApplication is a convenience annotation that implies a few others like @Configuration, @EnableAutoConfiguration and @ComponentScan. In more complex projects you most likely will declare each one individually to set a few parameters.
Now just execute the "mvn spring-boot:run" directive to run your application or "mvn package" to create a executable JAR file.
Subscribe to:
Posts (Atom)
Blog Archive
- November 2015 (3)