Skip to content

Commit 1cf3373

Browse files
committed
Add example project
1 parent eccdcc4 commit 1cf3373

File tree

7 files changed

+593
-0
lines changed

7 files changed

+593
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.5/apache-maven-3.9.5-bin.zip

README.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# DPE University Training
2+
3+
[<img width="10%" height="10%" src="https://user-images.githubusercontent.com/120980/174325546-8558160b-7f16-42cb-af0f-511849f22ebc.png">](https://dpeuniversity.gradle.com/)
4+
Checkout all the **free** Maven, Gradle, and DPE courses at the [DPE University][dpe-university]!
5+
6+
## Maven Build Cache Deep Dive - Lab 01: Getting Started
7+
8+
This is a hands-on exercise to go along with the [Maven Build Cache Deep Dive][course-url] training module. In this exercise you will go over the following:
9+
10+
- How to use a local build cache to speed up a Maven Build.
11+
- Adding the Develocity Maven Extension to a project.
12+
- Basic usage of a build cache.
13+
14+
## Prerequisites
15+
16+
- Finished going through the _Local Build Cache_ section in Maven Build Cache Deep Dive.
17+
- Java 11+
18+
19+
Steps
20+
-----
21+
22+
1. Apply the latest version of the Develocity Maven Extension to the project.
23+
24+
Run the command:
25+
```shell
26+
./mvnw com.gradle:develocity-maven-extension:1.21.4:init \
27+
-Ddevelocity.url=https://dpeuniversity-develocity.gradle.com
28+
```
29+
> [!NOTE]
30+
> As part of taking this **free** course, you have access to a training instance of Develocity located at:
31+
> ```
32+
> https://dpeuniversity-develocity.gradle.com/
33+
> ```
34+
> [Sign in][develocity-url] to this Develocity server using the same account you use for the DPE University.
35+
>
36+
> This server is configured so users can only access the Build Scan® and Build Cache entries they publish.
37+
38+
See the [getting started guide](https://docs.gradle.com/enterprise/maven-extension/#getting_set_up) for details.
39+
40+
2. Create an access key to access the Develocity server.
41+
42+
Run the command:
43+
```shell
44+
./mvnw com.gradle:develocity-maven-extension:1.21.4:provision-access-key
45+
```
46+
47+
> [!TIP]
48+
> For more ways to authenticate, see the [authentication guide](https://docs.gradle.com/enterprise/maven-extension/#authenticating_with_gradle_enterprise) to see how to provide credentials.
49+
50+
3. Configure the project to **only** use the local build cache. **Disable** the remote build cache.
51+
See how to set up `develocity.xml` in the [getting started guide](https://docs.gradle.com/enterprise/maven-extension/#getting_set_up).
52+
- See the [configuring the local cache](https://docs.gradle.com/enterprise/maven-extension/#configuring_the_local_cache) guide to configure local cache. (Enabled by default)
53+
- See the [disabling the remote cache](https://docs.gradle.com/enterprise/maven-extension/#disabling_the_remote_cache) guide to **disable** the remote cache.
54+
55+
4. Ensure local cache doesn't exist. Best way to do this is to delete `~/.m2/.develocity/build-cache`
56+
57+
```shell
58+
rm -rf ~/.m2/.develocity/build-cache
59+
```
60+
61+
5. Build and run the application with local caching enabled. Enable debug logging for the build cache to see the details. The command is:
62+
63+
```shell
64+
./mvnw clean verify exec:java -Dorg.slf4j.simpleLogger.log.develocity.goal.cache=debug
65+
```
66+
67+
On the first run, the build cache directory is created and populated with the outputs of the compilation operation. Identify the relevant log outputs.
68+
69+
6. Run again without changes. The log output contains two messages that indicated that the outputs were pulled from the cache. What would happen if you ran the same command multiple times?
70+
71+
7. What behavior would you expect if you deleted the directory `~/.m2/.develocity/build-cache` and re-ran the same command.
72+
73+
8. Configure the local cache to a non-standard directory and set the number of days that unused entries will be garbage collected to 30 days.
74+
- See the [local cache configuration](https://docs.gradle.com/enterprise/maven-extension/#changing_the_local_cache_directory) guide to set the directory. You can simply set it to `build-cache` which will create the cache directory in the current project.
75+
- See the [local cache cleanup](https://docs.gradle.com/enterprise/maven-extension/#Configuring_local_cache_cleanup) guide to set the cleanup `retention` and `interval` values.
76+
77+
## Solution Reference
78+
79+
To see the solution to the lab, check out the [`solution`](https://github.com/gradle/getting-started-maven-build-cache-lab/commit/solution) branch of this project.
80+
81+
## More Free Labs & Courses
82+
83+
Be sure to check out our other **free** [courses][dpe-university] and [labs](https://github.com/gradle?q=dpeuni)!
84+
85+
**Related courses:**
86+
- [Maven - Build Cache Deep Dive][course-url]
87+
- [Maven - Maintaining an Optimized Build Cache](https://dpeuniversity.gradle.com/c/42cf9d626302011526c4a0536b26af929b5bef58)
88+
- [Develocity - How to Use Build Scans](https://dpeuniversity.gradle.com/c/0b0b3e4a8d21709ff39074e9962eee6ca4276dc1)
89+
90+
**Related labs:**
91+
- [Lab 01 - Using the local build cache](https://github.com/gradle/getting-started-maven-build-cache-lab)
92+
- [Lab 02 - Missing Inputs With Build Caching](https://github.com/gradle/missing-inputs-maven-build-cache-lab)
93+
- [Lab 03 - Add Build Cache Support to any Maven Plugin](https://github.com/gradle/caching-any-plugin-maven-build-cache-lab)
94+
- [Lab 04 - Handling Cache Misses with Normalization](https://github.com/gradle/cache-misses-maven-build-cache-lab)
95+
- [Lab 05 - Outputs Overwrite Inputs](https://github.com/gradle/outputs-overwrite-inputs-maven-build-cache-lab)
96+
- [Lab 06 - Maintaining an Optimized Build Cache](https://github.com/gradle/maintaining-optimized-cache-maven-build-cache-lab)
97+
98+
[course-url]: https://dpeuniversity.gradle.com/c/47262fea1e74b719afb590d8cb3f8280bf2af732
99+
[dpe-university]: https://dpeuniversity.gradle.com/
100+
[develocity-url]: https://dpeuniversity-develocity.gradle.com/

mvnw

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
#!/bin/sh
2+
# ----------------------------------------------------------------------------
3+
# Licensed to the Apache Software Foundation (ASF) under one
4+
# or more contributor license agreements. See the NOTICE file
5+
# distributed with this work for additional information
6+
# regarding copyright ownership. The ASF licenses this file
7+
# to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance
9+
# with the License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing,
14+
# software distributed under the License is distributed on an
15+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
# KIND, either express or implied. See the License for the
17+
# specific language governing permissions and limitations
18+
# under the License.
19+
# ----------------------------------------------------------------------------
20+
21+
# ----------------------------------------------------------------------------
22+
# Apache Maven Wrapper startup batch script, version 3.2.0
23+
#
24+
# Optional ENV vars
25+
# -----------------
26+
# JAVA_HOME - location of a JDK home dir, required when download maven via java source
27+
# MVNW_REPOURL - repo url base for downloading maven distribution
28+
# MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven
29+
# MVNW_VERBOSE - true: enable verbose log; debug: trace the mvnw script; others: silence the output
30+
# ----------------------------------------------------------------------------
31+
32+
set -euf
33+
[ "${MVNW_VERBOSE-}" != debug ] || set -x
34+
35+
# OS specific support.
36+
native_path() { printf %s\\n "$1"; }
37+
case "$(uname)" in
38+
(CYGWIN*|MINGW*) [ -z "${JAVA_HOME-}" ] || JAVA_HOME="$(cygpath --unix "$JAVA_HOME")"
39+
native_path() { cygpath --path --windows "$1"; } ;;
40+
esac
41+
42+
# set JAVACMD and JAVACCMD
43+
set_java_home() {
44+
# For Cygwin and MinGW, ensure paths are in Unix format before anything is touched
45+
if [ -n "${JAVA_HOME-}" ] ; then
46+
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
47+
# IBM's JDK on AIX uses strange locations for the executables
48+
JAVACMD="$JAVA_HOME/jre/sh/java"
49+
JAVACCMD="$JAVA_HOME/jre/sh/javac"
50+
else
51+
JAVACMD="$JAVA_HOME/bin/java"
52+
JAVACCMD="$JAVA_HOME/bin/javac"
53+
54+
if [ ! -x "$JAVACMD" ] || [ ! -x "$JAVACCMD" ] ; then
55+
echo "The JAVA_HOME environment variable is not defined correctly, so mvnw cannot run." >&2
56+
echo "JAVA_HOME is set to \"$JAVA_HOME\", but \"\$JAVA_HOME/bin/java\" or \"\$JAVA_HOME/bin/javac\" does not exist." >&2
57+
return 1
58+
fi
59+
fi
60+
else
61+
JAVACMD="$('set' +e; 'unset' -f command 2>/dev/null; 'command' -v java)" || :
62+
JAVACCMD="$('set' +e; 'unset' -f command 2>/dev/null; 'command' -v javac)" || :
63+
64+
if [ ! -x "${JAVACMD-}" ] || [ ! -x "${JAVACCMD-}" ] ; then
65+
echo "The java/javac command does not exist in PATH nor is JAVA_HOME set, so mvnw cannot run." >&2
66+
return 1
67+
fi
68+
fi
69+
}
70+
71+
# hash string like Java String::hashCode
72+
hash_string() {
73+
str="${1:-}" h=0
74+
while [ -n "$str" ]; do
75+
h=$(( ( h * 31 + $(LC_CTYPE=C printf %d "'$str") ) % 4294967296 ))
76+
str="${str#?}"
77+
done
78+
printf %x\\n $h
79+
}
80+
81+
verbose() { :; }
82+
[ "${MVNW_VERBOSE-}" != true ] || verbose() { printf %s\\n "${1-}"; }
83+
84+
die() {
85+
printf %s\\n "$1" >&2
86+
exit 1
87+
}
88+
89+
# parse distributionUrl and optional distributionSha256Sum, requires .mvn/wrapper/maven-wrapper.properties
90+
while IFS="=" read -r key value; do
91+
case "${key-}" in
92+
distributionUrl) distributionUrl="${value-}" ;;
93+
distributionSha256Sum) distributionSha256Sum="${value-}" ;;
94+
esac
95+
done < "${0%/*}/.mvn/wrapper/maven-wrapper.properties"
96+
[ -n "${distributionUrl-}" ] || die "cannot read distributionUrl property in ${0%/*}/.mvn/wrapper/maven-wrapper.properties"
97+
98+
99+
case "${distributionUrl##*/}" in
100+
(maven-mvnd-*bin.*)
101+
MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/
102+
case "${PROCESSOR_ARCHITECTURE-}${PROCESSOR_ARCHITEW6432-}:$(uname -a)" in
103+
(*AMD64:CYGWIN*|*AMD64:MINGW*) distributionPlatform=windows-amd64 ;;
104+
(:Darwin*x86_64) distributionPlatform=darwin-amd64 ;;
105+
(:Darwin*arm64) distributionPlatform=darwin-aarch64 ;;
106+
(:Linux*x86_64*) distributionPlatform=linux-amd64 ;;
107+
(*) echo "Cannot detect native platform for mvnd on $(uname)-$(uname -m), use pure java version" >&2
108+
distributionPlatform=linux-amd64
109+
;;
110+
esac
111+
distributionUrl="${distributionUrl%-bin.*}-$distributionPlatform.zip"
112+
;;
113+
(maven-mvnd-*) MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ ;;
114+
(*) MVN_CMD="mvn${0##*/mvnw}" _MVNW_REPO_PATTERN=/org/apache/maven/ ;;
115+
esac
116+
117+
# apply MVNW_REPOURL and calculate MAVEN_HOME
118+
# maven home pattern: ~/.m2/wrapper/dists/{apache-maven-<version>,maven-mvnd-<version>-<platform>}/<hash>
119+
[ -z "${MVNW_REPOURL-}" ] || distributionUrl="$MVNW_REPOURL$_MVNW_REPO_PATTERN${distributionUrl#*"$_MVNW_REPO_PATTERN"}"
120+
distributionUrlName="${distributionUrl##*/}"
121+
distributionUrlNameMain="${distributionUrlName%.*}"
122+
distributionUrlNameMain="${distributionUrlNameMain%-bin}"
123+
MAVEN_HOME="$HOME/.m2/wrapper/dists/${distributionUrlNameMain-}/$(hash_string "$distributionUrl")"
124+
125+
exec_maven() {
126+
unset MVNW_VERBOSE MVNW_USERNAME MVNW_PASSWORD MVNW_REPOURL || :
127+
exec "$MAVEN_HOME/bin/$MVN_CMD" "$@" || die "cannot exec $MAVEN_HOME/bin/$MVN_CMD"
128+
}
129+
130+
if [ -d "$MAVEN_HOME" ]; then
131+
verbose "found existing MAVEN_HOME at $MAVEN_HOME"
132+
exec_maven "$@"
133+
fi
134+
135+
case "${distributionUrl-}" in
136+
(*?-bin.zip|*?maven-mvnd-?*-?*.zip) ;;
137+
(*) die "distributionUrl is not valid, must match *-bin.zip or maven-mvnd-*.zip, but found '${distributionUrl-}'" ;;
138+
esac
139+
140+
# prepare tmp dir
141+
if TMP_DOWNLOAD_DIR="$(mktemp -d)" && [ -d "$TMP_DOWNLOAD_DIR" ]; then
142+
clean() { rm -rf -- "$TMP_DOWNLOAD_DIR"; }
143+
trap clean HUP INT TERM EXIT
144+
else
145+
die "cannot create temp dir"
146+
fi
147+
148+
mkdir -p -- "${MAVEN_HOME%/*}"
149+
150+
# Download and Install Apache Maven
151+
verbose "Couldn't find MAVEN_HOME, downloading and installing it ..."
152+
verbose "Downloading from: $distributionUrl"
153+
verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName"
154+
155+
# select .zip or .tar.gz
156+
if ! command -v unzip >/dev/null; then
157+
distributionUrl="${distributionUrl%.zip}.tar.gz"
158+
distributionUrlName="${distributionUrl##*/}"
159+
fi
160+
161+
# verbose opt
162+
__MVNW_QUIET_WGET=--quiet __MVNW_QUIET_CURL=--silent __MVNW_QUIET_UNZIP=-q __MVNW_QUIET_TAR=''
163+
[ "${MVNW_VERBOSE-}" != true ] || __MVNW_QUIET_WGET='' __MVNW_QUIET_CURL='' __MVNW_QUIET_UNZIP='' __MVNW_QUIET_TAR=v
164+
165+
# normalize http auth
166+
case "${MVNW_PASSWORD:+has-password}" in
167+
'') MVNW_USERNAME='' MVNW_PASSWORD='' ;;
168+
has-password) [ -n "${MVNW_USERNAME-}" ] || MVNW_USERNAME='' MVNW_PASSWORD='' ;;
169+
esac
170+
171+
if [ -z "${MVNW_USERNAME-}" ] && command -v wget > /dev/null; then
172+
verbose "Found wget ... using wget"
173+
wget ${__MVNW_QUIET_WGET:+"$__MVNW_QUIET_WGET"} "$distributionUrl" -O "$TMP_DOWNLOAD_DIR/$distributionUrlName"
174+
elif [ -z "${MVNW_USERNAME-}" ] && command -v curl > /dev/null; then
175+
verbose "Found curl ... using curl"
176+
curl ${__MVNW_QUIET_CURL:+"$__MVNW_QUIET_CURL"} -f -L -o "$TMP_DOWNLOAD_DIR/$distributionUrlName" "$distributionUrl"
177+
elif set_java_home; then
178+
verbose "Falling back to use Java to download"
179+
javaSource="$TMP_DOWNLOAD_DIR/Downloader.java"
180+
targetZip="$TMP_DOWNLOAD_DIR/$distributionUrlName"
181+
cat > "$javaSource" <<-END
182+
public class Downloader extends java.net.Authenticator
183+
{
184+
protected java.net.PasswordAuthentication getPasswordAuthentication()
185+
{
186+
return new java.net.PasswordAuthentication( System.getenv( "MVNW_USERNAME" ), System.getenv( "MVNW_PASSWORD" ).toCharArray() );
187+
}
188+
public static void main( String[] args ) throws Exception
189+
{
190+
setDefault( new Downloader() );
191+
java.nio.file.Files.copy( new java.net.URL( args[0] ).openStream(), java.nio.file.Paths.get( args[1] ).toAbsolutePath().normalize() );
192+
}
193+
}
194+
END
195+
# For Cygwin/MinGW, switch paths to Windows format before running javac and java
196+
verbose " - Compiling Downloader.java ..."
197+
"$(native_path "$JAVACCMD")" "$(native_path "$javaSource")"
198+
verbose " - Running Downloader.java ..."
199+
"$(native_path "$JAVACMD")" -cp "$(native_path "$TMP_DOWNLOAD_DIR")" Downloader "$distributionUrl" "$(native_path "$targetZip")"
200+
fi
201+
202+
# If specified, validate the SHA-256 sum of the Maven distribution zip file
203+
if [ -n "${distributionSha256Sum-}" ]; then
204+
distributionSha256Result=false
205+
if [ "$MVN_CMD" = mvnd.sh ]; then
206+
echo "Checksum validation is not supported for maven-mvnd." >&2
207+
echo "Please disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2
208+
exit 1
209+
elif command -v sha256sum > /dev/null; then
210+
if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | sha256sum -c > /dev/null 2>&1; then
211+
distributionSha256Result=true
212+
fi
213+
elif command -v shasum > /dev/null; then
214+
if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | shasum -a 256 -c > /dev/null 2>&1; then
215+
distributionSha256Result=true
216+
fi
217+
else
218+
echo "Checksum validation was requested but neither 'sha256sum' or 'shasum' are available." >&2
219+
echo "Please install either command, or disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2
220+
exit 1
221+
fi
222+
if [ $distributionSha256Result = false ]; then
223+
echo "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised." >&2
224+
echo "If you updated your Maven version, you need to update the specified distributionSha256Sum property." >&2
225+
exit 1
226+
fi
227+
fi
228+
229+
# unzip and move
230+
if command -v unzip > /dev/null; then
231+
unzip ${__MVNW_QUIET_UNZIP:+"$__MVNW_QUIET_UNZIP"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -d "$TMP_DOWNLOAD_DIR"
232+
else
233+
tar xzf${__MVNW_QUIET_TAR:+"$__MVNW_QUIET_TAR"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -C "$TMP_DOWNLOAD_DIR"
234+
fi
235+
printf %s\\n "$distributionUrl" > "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain/mvnw.url"
236+
mv -- "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" "$MAVEN_HOME" || [ -d "$MAVEN_HOME" ] || die "fail to move MAVEN_HOME"
237+
238+
clean || :
239+
exec_maven "$@"

0 commit comments

Comments
 (0)