Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add makefile to spring boot qs #762

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Use Java 17 (LTS) in maven jenkins-agent and spring boot qs ([#651](https://github.com/opendevstack/ods-quickstarters/pull/651))
- ODS AMI build fails due to failing jacoco report generation in springboot quickstarter ([#700](https://github.com/opendevstack/ods-quickstarters/pull/700))
- terraform agent sops/age added ([#730](https://github.com/opendevstack/ods-quickstarters/issues/730))
- Allow local project creation of quickstarter be-java-springboot ([#761](https://github.com/opendevstack/ods-quickstarters/pull/761))

### Fixed

Expand Down
49 changes: 8 additions & 41 deletions be-java-springboot/Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,52 +22,19 @@ odsQuickstarterPipeline(
dir(context.targetDir) {
def springBootVersion = '2.6.1.RELEASE'
echo "--- create spring boot (v${springBootVersion}) project via spring initializr ---"

sh "curl https://start.spring.io/starter.tgz \
-d type=gradle-project \
-d language=java \
-d dependencies='web,data-rest,restdocs,data-jpa,h2,security,devtools' \
-d bootVersion=${springBootVersion} \
-d packaging=jar \
-d javaVersion=17 \
-d groupId=${context.projectId} \
-d artifactId=${context.componentId} \
-d name=${context.componentId} \
-d description='OpenDevStack%20Demo%20Project%20for%20Spring%20Boot' \
-d packageName='${context.packageName}' \
| tar -zxvf - "
sh "make create-project TARGET_DIR=. SPRING_BOOT_CLI_VERSION=${springBootVersion} PROJECT_ID=${context.projectId} COMPONENT_ID=${context.componentId} PACKAGE_NAME=${context.packageName}"

echo "--- configure application.properties ---"
sh "make add-sb-app-properties TARGET_DIR=."

sh "echo 'spring.profiles.active: dev' > src/main/resources/application.properties"
sh "echo 'spring.jpa.database: HSQL' > src/main/resources/application-dev.properties"
}

echo "--- customise build.gradle ---"

// enforce minimal log4j2 version to avoid (CVE-2021-44228)
sh "sed -i -e '/ext {/{r ${context.sourceDir}/templates/gradle-minimal-log4j2-version.template' -e 'd' -e '}' ${context.targetDir}/build.gradle"

// add nexus credential settings
sh "cat ${context.sourceDir}/templates/gradle-buildscript.template ${context.targetDir}/build.gradle >out && mv out ${context.targetDir}/build.gradle"
echo "--- customise build.gradle ---"
sh "pwd && ls -lart"
sh "make customise-build-gradle SOURCE_DIR=../${context.sourceDir} TARGET_DIR=."
sh "rm Makefile"

// insert required plugins
sh "sed -i -e '/plugins {/{r ${context.sourceDir}/templates/gradle-plugins.template' -e 'd' -e '}' ${context.targetDir}/build.gradle"

// insert nexus repos
sh "sed -i -e '/mavenCentral()/{r ${context.sourceDir}/templates/gradle-repositories.template' -e 'd' -e '}' ${context.targetDir}/build.gradle"

// insert test settings
sh "sed -i -e '/test {/{r ${context.sourceDir}/templates/gradle-plugin-test.template' -e 'd' -e '}' ${context.targetDir}/build.gradle"

// append bootJar settings
sh "cat ${context.sourceDir}/templates/gradle-plugin-bootjar.template >> ${context.targetDir}/build.gradle"

// append jacoco settings
sh "cat ${context.sourceDir}/templates/gradle-plugin-jacoco.template >> ${context.targetDir}/build.gradle"
echo "--- end stage Build spring project ---"
}

// append maven-publish settings
sh "cat ${context.sourceDir}/templates/gradle-maven-publish.template >> ${context.targetDir}/build.gradle"
}

odsQuickstarterStageCopyFiles(context)
Expand Down
66 changes: 66 additions & 0 deletions be-java-springboot/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
SHELL = /bin/bash
.SHELLFLAGS := -eu -o pipefail -c
.DELETE_ON_ERROR:
MAKEFLAGS += --warn-undefined-variables
MAKEFLAGS += --no-builtin-rules

SOURCE_DIR=${sourceDir}
TARGET_DIR=${targetDir}
SPRING_BOOT_CLI_VERSION=${springBootVersion}
PROJECT_ID=${projectId}
COMPONENT_ID=${componentId}
PACKAGE_NAME=${packageName}

# create-project
create-project:
mkdir -p ${TARGET_DIR}
cd ${TARGET_DIR} && curl https://start.spring.io/starter.tgz \
-d type=gradle-project \
-d language=java \
-d dependencies='web,jersey,data-jpa,h2,data-rest,restdocs,security' \
-d bootVersion=${SPRING_BOOT_CLI_VERSION} \
-d packaging=jar \
-d javaVersion=17 \
-d groupId=${PROJECT_ID} \
-d artifactId=${COMPONENT_ID} \
-d name=${COMPONENT_ID} \
-d description='OpenDevStack%20Demo%20Project%20for%20Spring%20Boot' \
-d packageName='${PACKAGE_NAME}' \
| tar -zxvf -

.PHONY: create-project
# add-sb-app-properties
add-sb-app-properties:
echo 'spring.profiles.active: dev' > ${TARGET_DIR}/src/main/resources/application.properties
echo 'spring.jpa.database: HSQL' > ${TARGET_DIR}/src/main/resources/application-dev.properties
.PHONY: add-sb-app-properties
# customise-build-gradle
customise-build-gradle:
echo "called customise-build-gradle..."

echo "...enforce minimal log4j2 version to avoid (CVE-2021-44228)"
sed -i -e '/ext {/{r ${SOURCE_DIR}/templates/gradle-minimal-log4j2-version.template' -e 'd' -e '}' ${TARGET_DIR}/build.gradle

echo "...add nexus credential settings"
cat ${SOURCE_DIR}/templates/gradle-buildscript.template ${TARGET_DIR}/build.gradle >out && mv out ${TARGET_DIR}/build.gradle

echo "...insert required plugins"
sed -i -e '/plugins {/{r ${SOURCE_DIR}/templates/gradle-plugins.template' -e 'd' -e '}' ${TARGET_DIR}/build.gradle

echo "...insert nexus repos"
sed -i -e '/mavenCentral()/{r ${SOURCE_DIR}/templates/gradle-repositories.template' -e 'd' -e '}' ${TARGET_DIR}/build.gradle

echo "...insert test settings"
sed -i -e '/test {/{r ${SOURCE_DIR}/templates/gradle-plugin-test.template' -e 'd' -e '}' ${TARGET_DIR}/build.gradle

echo "...append bootJar settings"
cat ${SOURCE_DIR}/templates/gradle-plugin-bootjar.template >> ${TARGET_DIR}/build.gradle

echo "...append jacoco settings"
cat ${SOURCE_DIR}/templates/gradle-plugin-jacoco.template >> ${TARGET_DIR}/build.gradle

echo "...append maven-publish settings"
cat ${SOURCE_DIR}/templates/gradle-maven-publish.template >> ${TARGET_DIR}/build.gradle

.PHONY: customise-build-gradle

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for using this locally it would be nice to have an additional target that does all three existing targets in one, basically mimicking the jenkins stage where they are being called in.
And if you want to be fancy you could add a help target ;-)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case you have no help target at hand, I used this code once in the past:

### Based on https://gist.github.com/prwhite/8168133#gistcomment-2278355

# COLORS
GREEN  := $(shell tput -Txterm setaf 2)
YELLOW := $(shell tput -Txterm setaf 3)
WHITE  := $(shell tput -Txterm setaf 7)
RESET  := $(shell tput -Txterm sgr0)

## Show this help
help:
	@echo ''
	@echo 'Usage:'
	@echo '  make <${GREEN}target${RESET}>'
	@echo ''
	@echo 'Targets:'
	@awk '/^[0-9a-zA-Z_-]+:|^# .*/ { \
		helpMessage = match(lastLine, /^## (.*)/); \
		if (helpMessage) { \
			helpCommand = substr($$1, 0, index($$1, ":")-1); \
			helpMessage = substr(lastLine, RSTART + 3, RLENGTH); \
			printf "  ${GREEN}%-30s${RESET} %s\n", helpCommand, helpMessage; \
		} else { \
			printf "\n"; \
		} \
	} \
	{ lastLine = $$0 }' $(MAKEFILE_LIST)
.PHONY: help

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI I've switched to this short version lately: https://github.com/opendevstack/ods-pipeline/blob/master/Makefile#L15

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI I've switched to this short version lately: https://github.com/opendevstack/ods-pipeline/blob/master/Makefile#L15

cool! but this does require having awk installed, no? Not every unix distro has it installed out of the box AFAIK.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait, I think I'm mixing this up now with another unix tool

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool, I'll add this and also one target that calls all other.
@michaelsauter @renedupont do you think it might be worth to rename the target names to reflect some sort of standard targets/interface? Like create-project (calls all targets), create-project-template, update-project-template, etc...?

Copy link
Member

@renedupont renedupont Mar 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes why not
you only have to come up with names that fit all quickstarters :)

It is already hard for this qs here - if I see it right you would rename the current create-project target into create-project-template, but from my point of view what is actually happening in their is not a creation of a template but rather the initialization of the project (the service is calles spring initializr).