{"id":238009,"date":"2022-02-11T06:27:00","date_gmt":"2022-02-11T11:27:00","guid":{"rendered":"https:\/\/wordpress-756359-3782526.cloudwaysapps.com\/?p=238009"},"modified":"2025-05-30T07:30:31","modified_gmt":"2025-05-30T07:30:31","slug":"2022-02-11-springboot","status":"publish","type":"post","link":"https:\/\/www.travis-ci.com\/blog\/2022-02-11-springboot\/","title":{"rendered":"Travis and Spring Boot"},"content":{"rendered":"\n<p>In this series of tech blog Friday by Montana Mendy, we will learn how to run maven build goals, perform test coverage validation whether this be Coveralls, SonarCloud or Docker. Are you ready? I\u2019m ready. Let\u2019s jump in.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"getting-started\">Getting started<\/h2>\n\n\n\n<p>There\u2019s choices and choices. In this example I\u2019ll be using Maven, but you can obviously use Gradle as well. Next, you\u2019ll want to create a&nbsp;<a href=\"http:\/\/start.spring.io\/\">SpringBoot project<\/a>&nbsp;either using http:\/\/start.spring.io or alternatively use your IDE.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"create-your-travisyml-file\">Create your&nbsp;<code>.travis.yml<\/code>&nbsp;file<\/h2>\n\n\n\n<p>We have to enable your&nbsp;<code>.travis.yml<\/code>&nbsp;file and the way we do that is by the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>language: java\njdk: oraclejdk8<\/code><\/pre>\n\n\n\n<p>Now this just invokes Travis into your project, this is sufficient enough for Travis to see that there\u2019s a build that needs to be triggered, how Travis is architected Travis will run&nbsp;<code>mvn test -B<\/code>&nbsp;for building the project. If Travis finds&nbsp;<code>mvnw wrapper<\/code>&nbsp;then it will be used like&nbsp;<code>.\/mvnw test -B.<\/code>&nbsp;as you can see it\u2019s a bit recursive. You can instruct Travis to run different commands in your&nbsp;<code>script:<\/code>&nbsp;hook in your&nbsp;<code>.travis.yml<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"adding-coverage-checks-for-travis\">Adding Coverage Checks for Travis<\/h2>\n\n\n\n<p>Add the Maven specific JaCoCo plugin to&nbsp;<code>pom.xml<\/code>&nbsp;with flags that define what is the desired code coverage percentage, classes, etc:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;Plugin&gt;\n    &lt;GroupId&gt;org.jacoco&lt;\/GroupId&gt;\n    &lt;ArtifactId&gt;jacoco-maven-plugin&lt;\/ArtifactId&gt;\n    &lt;Version&gt;0.7.9&lt;\/Version&gt;\n    &lt;Configuration&gt;\n        &lt;Excludes&gt;\n            &lt;Exclude&gt;in\/sivalabs\/freelancerkit\/entities\/*&lt;\/Exclude&gt;\n            &lt;Exclude&gt;in\/sivalabs\/freelancerkit\/*Application&lt;\/Exclude&gt;\n        &lt;\/Excludes&gt;\n    &lt;\/Configuration&gt;\n    &lt;Executions&gt;\n        &lt;Execution&gt;\n            &lt;Id&gt;default-prepare-agent&lt;\/Id&gt;\n            &lt;Goals&gt;\n                &lt;Goal&gt;prepare-agent&lt;\/Goal&gt;\n            &lt;\/Goals&gt;\n        &lt;\/Execution&gt;\n        &lt;Execution&gt;\n            &lt;Id&gt;default-prepare-agent-integration&lt;\/Id&gt;\n            &lt;Goals&gt;\n                &lt;Goal&gt;prepare-agent-integration&lt;\/Goal&gt;\n            &lt;\/Goals&gt;\n        &lt;\/Execution&gt;\n        &lt;Execution&gt;\n            &lt;Id&gt;default-report&lt;\/Id&gt;\n            &lt;Phase&gt;verify&lt;\/Phase&gt;\n            &lt;Goals&gt;\n                &lt;Goal&gt;report&lt;\/Goal&gt;\n            &lt;\/Goals&gt;\n        &lt;\/Execution&gt;\n        &lt;Execution&gt;\n            &lt;Id&gt;default-report-integration&lt;\/Id&gt;\n            &lt;Goals&gt;\n                &lt;Goal&gt;report-integration&lt;\/Goal&gt;\n            &lt;\/Goals&gt;\n        &lt;\/Execution&gt;\n        &lt;Execution&gt;\n            &lt;Id&gt;default-check&lt;\/Id&gt;\n            &lt;Goals&gt;\n                &lt;Goal&gt;check&lt;\/Goal&gt;\n            &lt;\/Goals&gt;\n            &lt;Configuration&gt;\n                &lt;Rules&gt;\n                 &lt;Rule Implementation=\"Org.Jacoco.Maven.RuleConfiguration\"&gt;\n                        &lt;Element&gt;BUNDLE&lt;\/Element&gt;\n                        &lt;Limits&gt;\n                            &lt;Limit Implementation=\"Org.Jacoco.Report.Check.Limit\"&gt;\n                                &lt;Counter&gt;COMPLEXITY&lt;\/Counter&gt;\n                                &lt;Value&gt;COVEREDRATIO&lt;\/Value&gt;\n                                &lt;Minimum&gt;0.60&lt;\/Minimum&gt;\n                            &lt;\/Limit&gt;\n                        &lt;\/Limits&gt;\n                    &lt;\/Rule&gt;\n                &lt;\/Rules&gt;\n            &lt;\/Configuration&gt;\n        &lt;\/Execution&gt;\n    &lt;\/Executions&gt;\n&lt;\/Plugin&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"classifiers\">Classifiers<\/h2>\n\n\n\n<p>To fix the classifier issue, the workaround is to add the classifier configuration to&nbsp;<code>spring-boot-maven-plugin<\/code>&nbsp;as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;Plugin&gt;\n    &lt;GroupId&gt;org.springframework.boot&lt;\/GroupId&gt;\n    &lt;ArtifactId&gt;spring-boot-maven-plugin&lt;\/ArtifactId&gt;\n    &lt;Configuration&gt;\n        &lt;Classifier&gt;exec&lt;\/Classifier&gt;\n    &lt;\/Configuration&gt;\n&lt;\/Plugin&gt;<\/code><\/pre>\n\n\n\n<p>Now let\u2019s take a look at our&nbsp;<code>travis.yml<\/code>&nbsp;file, and be on the lookout for the&nbsp;<code>script:<\/code>&nbsp;hook:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>language: java\njdk: oraclejdk8\n  \nscript:\n- .\/mvnw clean install -B<\/code><\/pre>\n\n\n\n<p>You can see in our&nbsp;<code>script:<\/code>&nbsp;hook we\u2019ve added&nbsp;<code>.\/mvnw vlean install -B<\/code>&nbsp;and that\u2019s going to be key. Now let\u2019s get a&nbsp;<code>.travis.yml<\/code>&nbsp;that has SonarCloud. Now remember to set your&nbsp;<code>environment variables<\/code>&nbsp;you grabbed from SonarCloud and also add them to Travis, here\u2019s a&nbsp;<code>.travis.yml<\/code>&nbsp;demonstrating this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>language: java\njdk: oraclejdk8\n  \nenv:\n  global:\n  - secure: (ENV_VARS_HERE)\n  \naddons:\n  sonarcloud:\n    organization: \"montanamendy-from-travis\"\n    token:\n      secure: $SONAR_TOKEN\n  \nscript:\n- .\/mvnw clean install -B\n- .\/mvnw clean org.jacoco:jacoco-maven-plugin:prepare-agent package sonar:sonar\n<\/code><\/pre>\n\n\n\n<p>Now let\u2019s build a Dockerfile that mirrors essentially that&nbsp;<code>.travis.yml<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ROM frolvlad\/alpine-oraclejdk8:slim\nVOLUME \/tmp\nADD target\/freelancer-kit-0.0.1-SNAPSHOT.jar app.jar\nRUN sh -c 'touch \/app.jar'\nENV JAVA_OPTS=\"-Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8787,suspend=n\"\nEXPOSE 8080 8787\nENTRYPOINT &#91; \"sh\", \"-c\", \"java $JAVA_OPTS -Djava.security.egd=file:\/dev\/.\/urandom -Dspring.profiles.active=docker -jar \/app.jar\" ]<\/code><\/pre>\n\n\n\n<p>That Dockerfile is going to grab&nbsp;<code>alpine-oraclejdk8:slim<\/code>&nbsp;the&nbsp;<code>slim<\/code>&nbsp;flag is making sure it\u2019s grabbinb the slim version.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"deploying\">Deploying<\/h2>\n\n\n\n<p>Now let\u2019s say you\u2019re using Heroku for deployment, you\u2019ll need to grab all those credentials as well and add them to your&nbsp;<code>deploy:<\/code>&nbsp;hook. We can add your Heroku key and secrets by running the following command in the CLI:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>travis encrypt HEROKU_API_KEY=\u201dHEROKU_KEY\"<\/code><\/pre>\n\n\n\n<p>You\u2019ll now want to add the following to your&nbsp;<code>.travis.yml<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>deploy:\n  provider: heroku\n  api_key: $HEROKU_API_KEY\n  app: montana<\/code><\/pre>\n\n\n\n<p>Now that you\u2019ve added your Heroku secrets, it\u2019s time to see the final&nbsp;<code>.travis.yml<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo: required\nlanguage: java\njdk: oraclejdk8\n  \nservices:\n- docker\n  \nenv:\n  global:\n  - secure: \"encrypted-sonar-token\"\n  - secure: \"encrypted-dockerhub-username\"\n  - secure: \"encrypted-dockerhub-password\"\n  - secure: \"encrypted-heroku-api-key\"\n  - COMMIT=${TRAVIS_COMMIT::7}\n  \naddons:\n  sonarcloud:\n    organization: \"montanamendy-travis\"\n    token:\n      secure: $SONAR_TOKEN\n  \nscript:\n- .\/mvnw clean install -B\n- .\/mvnw clean org.jacoco:jacoco-maven-plugin:prepare-agent package sonar:sonar\n  \nafter_success:\n- docker login -u $DOCKER_USER -p $DOCKER_PASS\n- export TAG=`if &#91; \"$TRAVIS_BRANCH\" == \"master\" ]; then echo \"latest\"; else echo $TRAVIS_BRANCH&amp;amp;amp;amp;amp;amp;amp;lt;span data-mce-type=\"bookmark\" style=\"display: inline-block; width: 0px; overflow: hidden; line-height: 0;\" class=\"mce_SELRES_start\"&amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;lt;\/span&amp;amp;amp;amp;amp;amp;amp;gt;; fi`\n- export IMAGE_NAME=sivaprasadreddy\/freelancer-kit\n- docker build -t $IMAGE_NAME:$COMMIT .\n- docker tag $IMAGE_NAME:$COMMIT $IMAGE_NAME:$TAG\n- docker push $IMAGE_NAME\n  \ndeploy:\n  provider: heroku\n  api_key: $HEROKU_API_KEY\n  app: montanamendy-from-travis<\/code><\/pre>\n\n\n\n<p>If all is successful, you should see this in your Travis build log:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"258\" src=\"https:\/\/www.travis-ci.com\/wp-content\/uploads\/2022\/02\/153654615-eeb8a300-5b0b-4c74-8de5-a61b965b8199-1024x258-1.png\" alt=\"\" class=\"wp-image-242758\" srcset=\"https:\/\/www.travis-ci.com\/wp-content\/uploads\/2022\/02\/153654615-eeb8a300-5b0b-4c74-8de5-a61b965b8199-1024x258-1.png 1024w, https:\/\/www.travis-ci.com\/wp-content\/uploads\/2022\/02\/153654615-eeb8a300-5b0b-4c74-8de5-a61b965b8199-1024x258-1-300x76.png 300w, https:\/\/www.travis-ci.com\/wp-content\/uploads\/2022\/02\/153654615-eeb8a300-5b0b-4c74-8de5-a61b965b8199-1024x258-1-768x194.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>There you go, we just used unit testing, Spring Boot and Travis CI into our project. The integration was kind of easy wasn\u2019t it?<\/p>\n\n\n\n<p>As always if you have any questions, any questions at all, please email me at&nbsp;<a href=\"mailto:montana@travis-ci.org\">montana@travis-ci.org<\/a>.<\/p>\n\n\n\n<p>Happy building!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this series of tech blog Friday by Montana Mendy, we will learn how to run maven build goals, perform test coverage validation whether this be Coveralls, SonarCloud or Docker. Are you ready? I\u2019m ready. Let\u2019s jump in. Getting started There\u2019s choices and choices. In this example I\u2019ll be using Maven, but you can obviously [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_breakdance_hide_in_design_set":false,"_breakdance_tags":"","footnotes":""},"categories":[16],"tags":[7,19,20,5],"class_list":["post-238009","post","type-post","status-publish","format-standard","hentry","category-news","tag-community","tag-feature","tag-infrastructure","tag-news"],"_links":{"self":[{"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/posts\/238009","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/comments?post=238009"}],"version-history":[{"count":2,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/posts\/238009\/revisions"}],"predecessor-version":[{"id":242759,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/posts\/238009\/revisions\/242759"}],"wp:attachment":[{"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/media?parent=238009"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/categories?post=238009"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/tags?post=238009"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}