diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..eba1110 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2256be0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +.DS_Store +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties + +# Avoid ignoring Maven wrapper jar file (.jar files are usually ignored) +!/.mvn/wrapper/maven-wrapper.jar +.idea diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..9cde1f2 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,53 @@ +node { + stage('Configure') { + env.PATH = "${tool 'maven-3.3.9'}/bin:${env.PATH}" + version = '1.0.' + env.BUILD_NUMBER + currentBuild.displayName = version + + properties([ + buildDiscarder(logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '', numToKeepStr: '10')), + [$class: 'GithubProjectProperty', displayName: '', projectUrlStr: 'https://github.com/taz-jt/simple-java-maven-app/'], + pipelineTriggers([[$class: 'GitHubPushTrigger']]) + ]) + } + + stage('Checkout') { + git 'https://github.com/taz-jt/simple-java-maven-app' + } + + /* + stage('Version') { + sh "echo \'\ninfo.build.version=\'$version >> src/main/resources/application.properties || true" + sh "mvn -B -V -U -e versions:set -DnewVersion=$version" + } + */ + + stage('Build') { + sh 'mvn -B -DskipTests clean package' + } + + stage('Test') { + sh 'mvn test' + } + + stage('Archive') { + junit allowEmptyResults: true, testResults: '**/target/**/TEST*.xml' + } + + /* + stage('Deploy') { + // Depends on the 'Credentials Binding Plugin' + // (https://wiki.jenkins-ci.org/display/JENKINS/Credentials+Binding+Plugin) + withCredentials([[$class : 'UsernamePasswordMultiBinding', credentialsId: 'cloudfoundry', + usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) { + sh ''' + curl -L "https://cli.run.pivotal.io/stable?release=linux64-binary&source=github" | tar -zx + ./cf api https://api.run.pivotal.io + ./cf auth $USERNAME $PASSWORD + ./cf target -o bertjan-demo -s development + ./cf push + ''' + } + } + */ +} \ No newline at end of file diff --git a/Jenkinsfile-backup b/Jenkinsfile-backup new file mode 100644 index 0000000..8503331 --- /dev/null +++ b/Jenkinsfile-backup @@ -0,0 +1,28 @@ +pipeline { + agent { + docker { + image 'maven:3-alpine' + args '-v /root/.m2:/root/.m2' + } + } + options { + skipStagesAfterUnstable() + } + stages { + stage('Build') { + steps { + sh 'mvn -B -DskipTests clean package' + } + } + stage('Test') { + steps { + sh 'mvn test' + } + post { + always { + junit 'target/surefire-reports/*.xml' + } + } + } + } +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..40efacc --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +# simple-java-maven-app + +This repository is for the +[Build a Java app with Maven](https://jenkins.io/doc/tutorials/build-a-java-app-with-maven/) +tutorial in the [Jenkins User Documentation](https://jenkins.io/doc/). + +The repository contains a simple Java application which outputs the string +"Hello world!" and is accompanied by a couple of unit tests to check that the +main application works as expected. The results of these tests are saved to a +JUnit XML report. + +The `jenkins` directory contains an example of the `Jenkinsfile` (i.e. Pipeline) +you'll be creating yourself during the tutorial and the `scripts` subdirectory +contains a shell script with commands that are executed when Jenkins processes +the "Deliver" stage of your Pipeline. diff --git a/jenkins/Jenkinsfile b/jenkins/Jenkinsfile new file mode 100644 index 0000000..a70013f --- /dev/null +++ b/jenkins/Jenkinsfile @@ -0,0 +1,30 @@ +pipeline { + agent { + docker { + image 'maven:3-alpine' + args '-v /root/.m2:/root/.m2' + } + } + stages { + stage('Build') { + steps { + sh 'mvn -B -DskipTests clean package' + } + } + stage('Test') { + steps { + sh 'mvn test' + } + post { + always { + junit 'target/surefire-reports/*.xml' + } + } + } + stage('Deliver') { + steps { + sh './jenkins/scripts/deliver.sh' + } + } + } +} diff --git a/jenkins/scripts/deliver.sh b/jenkins/scripts/deliver.sh new file mode 100644 index 0000000..25b27dc --- /dev/null +++ b/jenkins/scripts/deliver.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash + +echo 'The following Maven command installs your Maven-built Java application' +echo 'into the local Maven repository, which will ultimately be stored in' +echo 'Jenkins''s local Maven repository (and the "maven-repository" Docker data' +echo 'volume).' +set -x +mvn jar:jar install:install help:evaluate -Dexpression=project.name +set +x + +echo 'The following complex command extracts the value of the element' +echo 'within of your Java/Maven project''s "pom.xml" file.' +set -x +NAME=`mvn help:evaluate -Dexpression=project.name | grep "^[^\[]"` +set +x + +echo 'The following complex command behaves similarly to the previous one but' +echo 'extracts the value of the element within instead.' +set -x +VERSION=`mvn help:evaluate -Dexpression=project.version | grep "^[^\[]"` +set +x + +echo 'The following command runs and outputs the execution of your Java' +echo 'application (which Jenkins built using Maven) to the Jenkins UI.' +set -x +java -jar target/${NAME}-${VERSION}.jar diff --git a/my-app.iml b/my-app.iml new file mode 100644 index 0000000..b033a64 --- /dev/null +++ b/my-app.iml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..9b7b6eb --- /dev/null +++ b/pom.xml @@ -0,0 +1,46 @@ + + 4.0.0 + com.mycompany.app + my-app + jar + 1.0-SNAPSHOT + my-app + http://maven.apache.org + + + junit + junit + 4.11 + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 1.6 + 1.6 + + + + org.apache.maven.plugins + maven-jar-plugin + 3.2.0 + + + + true + lib/ + com.mycompany.app.App + + + + + + + diff --git a/src/main/java/com/mycompany/app/App.java b/src/main/java/com/mycompany/app/App.java new file mode 100644 index 0000000..5a6d572 --- /dev/null +++ b/src/main/java/com/mycompany/app/App.java @@ -0,0 +1,21 @@ +package com.mycompany.app; + +/** + * Hello world! + */ +public class App +{ + + private final String message = "Hello World!"; + + public App() {} + + public static void main(String[] args) { + System.out.println(new App().getMessage()); + } + + private final String getMessage() { + return message; + } + +} diff --git a/src/test/java/com/mycompany/app/AppTest.java b/src/test/java/com/mycompany/app/AppTest.java new file mode 100644 index 0000000..908bac8 --- /dev/null +++ b/src/test/java/com/mycompany/app/AppTest.java @@ -0,0 +1,48 @@ +package com.mycompany.app; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.Before; +import org.junit.Test; +import org.junit.After; +import static org.junit.Assert.*; + +/** + * Unit test for simple App. + */ +public class AppTest +{ + + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + + @Before + public void setUpStreams() { + System.setOut(new PrintStream(outContent)); + } + + @Test + public void testAppConstructor() { + try { + new App(); + } catch (Exception e) { + fail("Construction failed."); + } + } + + @Test + public void testAppMain() + { + App.main(null); + try { + assertEquals("Hello World!" + System.getProperty("line.separator"), outContent.toString()); + } catch (AssertionError e) { + fail("\"message\" is not \"Hello World!\""); + } + } + + @After + public void cleanUpStreams() { + System.setOut(null); + } + +}