├── .gitattributes ├── .github └── dependabot.yml ├── .gitignore ├── LICENSE.txt ├── README.md ├── jenkins ├── Jenkinsfile └── scripts │ └── deliver.sh ├── pom.xml └── src ├── main └── java │ └── com │ └── mycompany │ └── app │ └── App.java └── test └── java └── com └── mycompany └── app └── AppTest.java /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # To get started with Dependabot version updates, you'll need to specify which 2 | # package ecosystems to update and where the package manifests are located. 3 | # Please see the documentation for all configuration options: 4 | # https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates 5 | 6 | version: 2 7 | updates: 8 | - package-ecosystem: "maven" 9 | directory: "/" # Location of package manifests 10 | schedule: 11 | interval: "weekly" 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | target/ 3 | pom.xml.tag 4 | pom.xml.releaseBackup 5 | pom.xml.versionsBackup 6 | pom.xml.next 7 | release.properties 8 | dependency-reduced-pom.xml 9 | buildNumber.properties 10 | .mvn/timing.properties 11 | 12 | # Avoid ignoring Maven wrapper jar file (.jar files are usually ignored) 13 | !/.mvn/wrapper/maven-wrapper.jar 14 | 15 | .idea 16 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Jenkins Documentation 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # simple-java-maven-app 2 | 3 | This repository is for the 4 | [Build a Java app with Maven](https://jenkins.io/doc/tutorials/build-a-java-app-with-maven/) 5 | tutorial in the [Jenkins User Documentation](https://jenkins.io/doc/). 6 | 7 | The repository contains a simple Java application which outputs the string 8 | "Hello world!" and is accompanied by a couple of unit tests to check that the 9 | main application works as expected. The results of these tests are saved to a 10 | JUnit XML report. 11 | 12 | The `jenkins` directory contains an example of the `Jenkinsfile` (i.e. Pipeline) 13 | you'll be creating yourself during the tutorial and the `jenkins/scripts` subdirectory 14 | contains a shell script with commands that are executed when Jenkins processes 15 | the "Deliver" stage of your Pipeline. 16 | -------------------------------------------------------------------------------- /jenkins/Jenkinsfile: -------------------------------------------------------------------------------- 1 | pipeline { 2 | agent any 3 | options { 4 | skipStagesAfterUnstable() 5 | } 6 | stages { 7 | stage('Build') { 8 | steps { 9 | sh 'mvn -B -DskipTests clean package' 10 | } 11 | } 12 | stage('Test') { 13 | steps { 14 | sh 'mvn test' 15 | } 16 | post { 17 | always { 18 | junit 'target/surefire-reports/*.xml' 19 | } 20 | } 21 | } 22 | stage('Deliver') { 23 | steps { 24 | sh './jenkins/scripts/deliver.sh' 25 | } 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /jenkins/scripts/deliver.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | echo 'The following Maven command installs your Maven-built Java application' 4 | echo 'into the local Maven repository, which will ultimately be stored in' 5 | echo 'Jenkins''s local Maven repository (and the "maven-repository" Docker data' 6 | echo 'volume).' 7 | set -x 8 | mvn jar:jar install:install help:evaluate -Dexpression=project.name 9 | set +x 10 | 11 | echo 'The following command extracts the value of the element' 12 | echo 'within of your Java/Maven project''s "pom.xml" file.' 13 | set -x 14 | NAME=`mvn -q -DforceStdout help:evaluate -Dexpression=project.name` 15 | set +x 16 | 17 | echo 'The following command behaves similarly to the previous one but' 18 | echo 'extracts the value of the element within instead.' 19 | set -x 20 | VERSION=`mvn -q -DforceStdout help:evaluate -Dexpression=project.version` 21 | set +x 22 | 23 | echo 'The following command runs and outputs the execution of your Java' 24 | echo 'application (which Jenkins built using Maven) to the Jenkins UI.' 25 | set -x 26 | java -jar target/${NAME}-${VERSION}.jar 27 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.mycompany.app 5 | my-app 6 | jar 7 | 1.0-SNAPSHOT 8 | my-app 9 | https://maven.apache.org 10 | 11 | 12 | org.junit.jupiter 13 | junit-jupiter-api 14 | 5.13.0 15 | test 16 | 17 | 18 | 19 | UTF-8 20 | 21 | 22 | 23 | 24 | 25 | org.apache.maven.plugins 26 | maven-compiler-plugin 27 | 3.14.0 28 | 29 | 17 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | org.apache.maven.plugins 38 | maven-jar-plugin 39 | 3.4.2 40 | 41 | 42 | 43 | true 44 | lib/ 45 | com.mycompany.app.App 46 | 47 | 48 | 49 | 50 | 51 | org.apache.maven.plugins 52 | maven-enforcer-plugin 53 | 3.5.0 54 | 55 | 56 | enforce-maven 57 | 58 | enforce 59 | 60 | 61 | 62 | 63 | [3.9.9,) 64 | 65 | 66 | [21,) 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | -------------------------------------------------------------------------------- /src/main/java/com/mycompany/app/App.java: -------------------------------------------------------------------------------- 1 | package com.mycompany.app; 2 | 3 | /** 4 | * Hello world! 5 | */ 6 | public class App { 7 | 8 | private static final String MESSAGE = "Hello World!"; 9 | 10 | public App() {} 11 | 12 | public static void main(String[] args) { 13 | System.out.println(MESSAGE); 14 | } 15 | 16 | public String getMessage() { 17 | return MESSAGE; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/com/mycompany/app/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.mycompany.app; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | { 12 | @Test 13 | public void testAppConstructor() { 14 | App app1 = new App(); 15 | App app2 = new App(); 16 | assertEquals(app1.getMessage(), app2.getMessage()); 17 | } 18 | 19 | @Test 20 | public void testAppMessage() 21 | { 22 | App app = new App(); 23 | assertEquals("Hello World!", app.getMessage()); 24 | } 25 | } 26 | --------------------------------------------------------------------------------