Skip to content

Commit 0ea1bce

Browse files
committed
Java Maven
0 parents  commit 0ea1bce

File tree

10 files changed

+237
-0
lines changed

10 files changed

+237
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.github/dependabot.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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"

.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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.

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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.

jenkins/Jenkinsfile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
}

jenkins/scripts/deliver.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 <name/> element'
12+
echo 'within <project/> 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 <version/> element within <project/> 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

pom.xml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.mycompany.app</groupId>
5+
<artifactId>my-app</artifactId>
6+
<packaging>jar</packaging>
7+
<version>1.0-SNAPSHOT</version>
8+
<name>my-app</name>
9+
<url>https://maven.apache.org</url>
10+
<dependencies>
11+
<dependency>
12+
<groupId>org.junit.jupiter</groupId>
13+
<artifactId>junit-jupiter-api</artifactId>
14+
<version>5.12.2</version>
15+
<scope>test</scope>
16+
</dependency>
17+
</dependencies>
18+
<properties>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
</properties>
21+
<build>
22+
<pluginManagement>
23+
<plugins>
24+
<plugin>
25+
<groupId>org.apache.maven.plugins</groupId>
26+
<artifactId>maven-compiler-plugin</artifactId>
27+
<version>3.14.0</version>
28+
<configuration>
29+
<release>17</release>
30+
</configuration>
31+
</plugin>
32+
</plugins>
33+
</pluginManagement>
34+
<plugins>
35+
<plugin>
36+
<!-- Build an executable JAR -->
37+
<groupId>org.apache.maven.plugins</groupId>
38+
<artifactId>maven-jar-plugin</artifactId>
39+
<version>3.4.2</version>
40+
<configuration>
41+
<archive>
42+
<manifest>
43+
<addClasspath>true</addClasspath>
44+
<classpathPrefix>lib/</classpathPrefix>
45+
<mainClass>com.mycompany.app.App</mainClass>
46+
</manifest>
47+
</archive>
48+
</configuration>
49+
</plugin>
50+
<plugin>
51+
<groupId>org.apache.maven.plugins</groupId>
52+
<artifactId>maven-enforcer-plugin</artifactId>
53+
<version>3.5.0</version>
54+
<executions>
55+
<execution>
56+
<id>enforce-maven</id>
57+
<goals>
58+
<goal>enforce</goal>
59+
</goals>
60+
<configuration>
61+
<rules>
62+
<requireMavenVersion>
63+
<version>[3.9.2,)</version>
64+
</requireMavenVersion>
65+
<requireJavaVersion>
66+
<version>[17,)</version>
67+
</requireJavaVersion>
68+
</rules>
69+
</configuration>
70+
</execution>
71+
</executions>
72+
</plugin>
73+
</plugins>
74+
</build>
75+
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
}

0 commit comments

Comments
 (0)