Search

Dark theme | Light theme

March 16, 2016

Spring Sweets: Create New Projects From a URL

To quickly start with a Spring project we can use the website start.spring.io. Via a user interface we can set project properties and at the end we have a project archive (Zip or gzipped Tar file) or build file (pom.xml or build.gradle). We can also directory access an URL to create the output files and we set the properties via request parameters. This way you can share a link with someone and if they click on it they will download the generated project archive or build files.

We can choose different base URLs depending on the type of project archive we want. To get a Zip file we use http://start.spring.io/starter.zip and to get a gzipped Tar file we use http://start.spring.io/starter.tgz. To create a Gradle build file with all the configuration for a project we use http://start.spring.io/build.gradle. For a Maven POM XML file we use the URL http://start.spring.io/pom.xml.

All output format support the same request parameters to set the project properties:

Request parameterDescriptionSample
dependenciesAdd Spring Boot Starters and dependencies to your applicationweb,security
styleAlias for dependenciesactuator,sleuth,eureka&20discovery
typeUsed for project archives. Use gradle-project or maven-projectgradle-project
nameName of projectdemo
descriptionDescription for the projectDemo%20project%20for%20Spring%20Boot
groupIdValue for groupId for publishing projectcom.example
artifactIdValue for artifactId for publishing projectdemo
versionVersion of the project1.0.0.DEVELOPMENT
bootVersionVersion for Spring Boot1.3.3
packagingPackaging for project (jar or war)jar
applicationNameName of the applicationdemo
languageLanguage can be Java, Groovy or KotlinGroovy
packageNameName for package for example codecom.example
javaVersionJava version for project1.8
baseDirBase directory in archivesample


Let's create a URL for a Groovy project with a Gradle build file where we have a dependency on web, security and actuator:

http://start.spring.io/build.gradle?dependencies=web,security,actuator&name=sample&description=Sample&20Project&version=1.0.0.DEVELOPMENT&bootVersion=1.3.3.RELEASE&javaVersion=1.8&language=groovy&packaging=jar

When we save the Gradle build file we have the following file contents:

buildscript {
 ext {
  springBootVersion = '1.3.3.RELEASE'
 }
 repositories {
  mavenCentral()
 }
 dependencies {
  classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 
 }
}

apply plugin: 'groovy'
apply plugin: 'eclipse'
apply plugin: 'spring-boot' 

jar {
 baseName = 'demo'
 version = '1.0.0.DEVELOPMENT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
 mavenCentral()
}


dependencies {
 compile('org.springframework.boot:spring-boot-starter-actuator')
 compile('org.springframework.boot:spring-boot-starter-security')
 compile('org.springframework.boot:spring-boot-starter-web')
 compile('org.codehaus.groovy:groovy')
 testCompile('org.springframework.boot:spring-boot-starter-test') 
}


eclipse {
 classpath {
   containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
   containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
 }
}

task wrapper(type: Wrapper) {
 gradleVersion = '2.9'
}

Instead of just a build file we want to create a sample project archive file. We use the following URL:

http://start.spring.io/starter.tgz?dependencies=web,security,actuator&name=sample&description=Sample&20Project&version=1.0.0.DEVELOPMENT&bootVersion=1.3.3.RELEASE&javaVersion=1.8&language=groovy&packaging=jar&type=gradle-project&baseDir=sample

Let's open the archive and see the contents. Notice we used the baseDir request parameter so when we unpack we get a new directory.

$ tar xf starter.tgz 
$ tree sample/
sample/
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── src
    ├── main
    │   ├── groovy
    │   │   └── com
    │   │       └── example
    │   │           └── SampleApplication.groovy
    │   └── resources
    │       ├── application.properties
    │       ├── static
    │       └── templates
    └── test
        └── groovy
            └── com
                └── example
                    └── SampleApplicationTests.groovy

14 directories, 8 files
$

We can also use wget, cUrl or Httpie to download a starter template from start.spring.io using request parameters.

Written with Spring Boot 1.3.3.