1+ name : Build and Test Java
2+
3+ on :
4+ workflow_call :
5+ inputs :
6+ java-version :
7+ description : ' Java version to use'
8+ default : ' 17'
9+ type : string
10+ java-distribution :
11+ description : ' Java distribution to use (temurin, zulu, adopt, etc.)'
12+ default : ' temurin'
13+ type : string
14+ build-tool :
15+ description : ' Build tool to use (gradle or maven)'
16+ default : ' gradle'
17+ type : string
18+ gradle-version :
19+ description : ' Gradle version to use (only applicable if build-tool is gradle)'
20+ default : ' wrapper'
21+ type : string
22+ gradle-build-task :
23+ description : ' Gradle build task to run'
24+ default : ' build'
25+ type : string
26+ gradle-build-file :
27+ description : ' Gradle build file to use'
28+ default : ' '
29+ type : string
30+ maven-version :
31+ description : ' Maven version to use (only applicable if build-tool is maven)'
32+ default : ' 3.9.5'
33+ type : string
34+ maven-goals :
35+ description : ' Maven goals to run'
36+ default : ' clean package'
37+ type : string
38+ maven-args :
39+ description : ' Additional Maven arguments'
40+ default : ' '
41+ type : string
42+ working-directory :
43+ description : ' Working directory where the build commands will be run'
44+ default : ' .'
45+ type : string
46+ upload-artifacts :
47+ description : ' Whether to upload build artifacts'
48+ default : true
49+ type : boolean
50+ artifacts-name :
51+ description : ' Name of the artifacts to upload'
52+ default : ' build-artifacts'
53+ type : string
54+ artifacts-path :
55+ description : ' Path to the artifacts to upload (relative to working-directory)'
56+ default : ' '
57+ type : string
58+
59+ jobs :
60+ build :
61+ runs-on : ubuntu-latest
62+ steps :
63+ - name : Checkout Repository
64+ uses : actions/checkout@v4
65+
66+ - name : Set up JDK
67+ uses : actions/setup-java@v4
68+ with :
69+ java-version : ${{ inputs.java-version }}
70+ distribution : ${{ inputs.java-distribution }}
71+ architecture : x64
72+
73+ # Gradle build
74+ - name : Setup Gradle
75+ if : ${{ inputs.build-tool == 'gradle' }}
76+ uses : gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0
77+ with :
78+ gradle-version : ${{ inputs.gradle-version }}
79+
80+ - name : Build with Gradle
81+ if : ${{ inputs.build-tool == 'gradle' }}
82+ working-directory : ${{ inputs.working-directory }}
83+ run : |
84+ if [ -n "${{ inputs.gradle-build-file }}" ]; then
85+ ./gradlew -b ${{ inputs.gradle-build-file }} ${{ inputs.gradle-build-task }}
86+ else
87+ ./gradlew ${{ inputs.gradle-build-task }}
88+ fi
89+
90+ # Maven build
91+ - name : Setup Maven
92+ if : ${{ inputs.build-tool == 'maven' }}
93+ uses : stCarolas/setup-maven@v5
94+ with :
95+ maven-version : ${{ inputs.maven-version }}
96+
97+ - name : Build with Maven
98+ if : ${{ inputs.build-tool == 'maven' }}
99+ working-directory : ${{ inputs.working-directory }}
100+ run : mvn ${{ inputs.maven-goals }} ${{ inputs.maven-args }}
101+
102+ # Determine artifacts path based on build tool if not explicitly provided
103+ - name : Set default artifacts path
104+ if : ${{ inputs.upload-artifacts && inputs.artifacts-path == '' }}
105+ id : set-artifacts-path
106+ run : |
107+ if [ "${{ inputs.build-tool }}" = "gradle" ]; then
108+ echo "path=build/libs" >> $GITHUB_OUTPUT
109+ else
110+ echo "path=target" >> $GITHUB_OUTPUT
111+ fi
112+
113+ # Upload artifacts
114+ - name : Upload build artifacts
115+ if : ${{ inputs.upload-artifacts }}
116+ uses : actions/upload-artifact@v4
117+ with :
118+ name : ${{ inputs.artifacts-name }}
119+ path : ${{ inputs.working-directory }}/${{ inputs.artifacts-path != '' && inputs.artifacts-path || steps.set-artifacts-path.outputs.path }}
0 commit comments