Skip to content

Commit

Permalink
First
Browse files Browse the repository at this point in the history
  • Loading branch information
mrastrom committed Feb 26, 2020
0 parents commit c545866
Show file tree
Hide file tree
Showing 11 changed files with 299 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
53 changes: 53 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -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
'''
}
}
*/
}
28 changes: 28 additions & 0 deletions Jenkinsfile-backup
Original file line number Diff line number Diff line change
@@ -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'
}
}
}
}
}
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
30 changes: 30 additions & 0 deletions jenkins/Jenkinsfile
Original file line number Diff line number Diff line change
@@ -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'
}
}
}
}
26 changes: 26 additions & 0 deletions jenkins/scripts/deliver.sh
Original file line number Diff line number Diff line change
@@ -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 <name/> element'
echo 'within <project/> 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 <version/> element within <project/> 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
16 changes: 16 additions & 0 deletions my-app.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_6">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.11" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
</component>
</module>
46 changes: 46 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>my-app</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.mycompany.app.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
21 changes: 21 additions & 0 deletions src/main/java/com/mycompany/app/App.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
48 changes: 48 additions & 0 deletions src/test/java/com/mycompany/app/AppTest.java
Original file line number Diff line number Diff line change
@@ -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);
}

}

0 comments on commit c545866

Please sign in to comment.