├── .gitattributes
├── .gitignore
├── 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
--------------------------------------------------------------------------------
/.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 |
--------------------------------------------------------------------------------
/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 `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 {
3 | docker {
4 | image 'maven:3-alpine'
5 | args '-v /root/.m2:/root/.m2'
6 | }
7 | }
8 | stages {
9 | stage('Build') {
10 | steps {
11 | sh 'mvn -B -DskipTests clean package'
12 | }
13 | }
14 | stage('Test') {
15 | steps {
16 | sh 'mvn test'
17 | }
18 | post {
19 | always {
20 | junit 'target/surefire-reports/*.xml'
21 | }
22 | }
23 | }
24 | stage('Deliver') {
25 | steps {
26 | sh './jenkins/scripts/deliver.sh'
27 | }
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/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 complex 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 help:evaluate -Dexpression=project.name | grep "^[^\[]"`
15 | set +x
16 |
17 | echo 'The following complex command behaves similarly to the previous one but'
18 | echo 'extracts the value of the element within instead.'
19 | set -x
20 | VERSION=`mvn help:evaluate -Dexpression=project.version | grep "^[^\[]"`
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 | http://maven.apache.org
10 |
11 |
12 | junit
13 | junit
14 | 4.13.2
15 | test
16 |
17 |
18 |
19 | UTF-8
20 |
21 |
22 |
23 |
24 |
25 | org.apache.maven.plugins
26 | maven-compiler-plugin
27 | 3.8.1
28 |
29 |
30 |
31 |
32 |
33 |
34 | org.apache.maven.plugins
35 | maven-jar-plugin
36 | 3.2.0
37 |
38 |
39 |
40 | true
41 | lib/
42 | com.mycompany.app.App
43 |
44 |
45 |
46 |
47 |
48 | org.apache.maven.plugins
49 | maven-enforcer-plugin
50 | 3.0.0-M3
51 |
52 |
53 | enforce-maven
54 |
55 | enforce
56 |
57 |
58 |
59 |
60 | [3.5.4,)
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
--------------------------------------------------------------------------------
/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 |
9 | private final String message = "Hello World!";
10 |
11 | public App() {}
12 |
13 | public static void main(String[] args) {
14 | System.out.println(new App().getMessage());
15 | }
16 |
17 | private final String getMessage() {
18 | return message;
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/src/test/java/com/mycompany/app/AppTest.java:
--------------------------------------------------------------------------------
1 | package com.mycompany.app;
2 |
3 | import java.io.ByteArrayOutputStream;
4 | import java.io.PrintStream;
5 | import org.junit.Before;
6 | import org.junit.Test;
7 | import org.junit.After;
8 | import static org.junit.Assert.*;
9 |
10 | /**
11 | * Unit test for simple App.
12 | */
13 | public class AppTest
14 | {
15 |
16 | private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
17 |
18 | @Before
19 | public void setUpStreams() {
20 | System.setOut(new PrintStream(outContent));
21 | }
22 |
23 | @Test
24 | public void testAppConstructor() {
25 | try {
26 | new App();
27 | } catch (Exception e) {
28 | fail("Construction failed.");
29 | }
30 | }
31 |
32 | @Test
33 | public void testAppMain()
34 | {
35 | App.main(null);
36 | try {
37 | assertEquals("Hello World!" + System.getProperty("line.separator"), outContent.toString());
38 | } catch (AssertionError e) {
39 | fail("\"message\" is not \"Hello World!\"");
40 | }
41 | }
42 |
43 | @After
44 | public void cleanUpStreams() {
45 | System.setOut(null);
46 | }
47 |
48 | }
49 |
--------------------------------------------------------------------------------