From 6adf88fc3d3747251432c24e8ba6d96b74e55b08 Mon Sep 17 00:00:00 2001 From: Guillem Hernandez Sola Date: Wed, 18 Jul 2018 17:07:19 +0200 Subject: [PATCH] First commit --- .gitattributes | 2 + .gitignore | 172 ++++++++++++++++++++++ .idea/.name | 1 + .idea/compiler.xml | 16 ++ .idea/vcs.xml | 6 + README.md | 15 ++ jenkins/Jenkinsfile | 25 ++++ jenkins/scripts/deliver.sh | 26 ++++ pom.xml | 46 ++++++ src/main/java/com/itnove/app/App.java | 21 +++ src/test/java/com/itnove/app/AppTest.java | 48 ++++++ 11 files changed, 378 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 .idea/.name create mode 100644 .idea/compiler.xml create mode 100644 .idea/vcs.xml create mode 100644 README.md create mode 100644 jenkins/Jenkinsfile create mode 100755 jenkins/scripts/deliver.sh create mode 100644 pom.xml create mode 100644 src/main/java/com/itnove/app/App.java create mode 100644 src/test/java/com/itnove/app/AppTest.java 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..7a323bf --- /dev/null +++ b/.gitignore @@ -0,0 +1,172 @@ + +# Created by https://www.gitignore.io/api/java,macos,maven,windows,intellij+iml,visualstudiocode + +### Intellij+iml ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/modules.xml +# .idea/*.iml +# .idea/modules + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +### Intellij+iml Patch ### +# Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023 + +*.iml +modules.xml +.idea/misc.xml +*.ipr + +### Java ### +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +### macOS ### +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +### Maven ### +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +.mvn/wrapper/maven-wrapper.jar + +### VisualStudioCode ### +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json + +### Windows ### +# Windows thumbnail cache files +Thumbs.db +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + + +# End of https://www.gitignore.io/api/java,macos,maven,windows,intellij+iml,visualstudiocode diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..c20e6be --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +my-app \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..7a6fec8 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ 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..5dd517b --- /dev/null +++ b/jenkins/Jenkinsfile @@ -0,0 +1,25 @@ +pipeline { + agent any + 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 100755 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/pom.xml b/pom.xml new file mode 100644 index 0000000..49be444 --- /dev/null +++ b/pom.xml @@ -0,0 +1,46 @@ + + 4.0.0 + com.itnove.app + my-app + jar + 18.07.23 + my-app + http://maven.apache.org + + + junit + junit + 4.12 + test + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.6.1 + + 1.8 + 1.8 + + + + + org.apache.maven.plugins + maven-jar-plugin + 3.0.2 + + + + true + lib/ + com.itnove.app.App + + + + + + + diff --git a/src/main/java/com/itnove/app/App.java b/src/main/java/com/itnove/app/App.java new file mode 100644 index 0000000..2575d74 --- /dev/null +++ b/src/main/java/com/itnove/app/App.java @@ -0,0 +1,21 @@ +package com.itnove.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/itnove/app/AppTest.java b/src/test/java/com/itnove/app/AppTest.java new file mode 100644 index 0000000..db3992e --- /dev/null +++ b/src/test/java/com/itnove/app/AppTest.java @@ -0,0 +1,48 @@ +package com.itnove.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); + } + +}