Skip to content

Commit 5c7562b

Browse files
authored
Merge pull request #344 from derms/dev
Create examples for disconnected (offline) projects that uses the plugins dsl #343
2 parents 889d6c9 + 4e5f559 commit 5c7562b

File tree

8 files changed

+455
-0
lines changed

8 files changed

+455
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Standalone Gradle deployer with ml-gradle
2+
3+
## Quickstart
4+
5+
Summary of the steps required. See sections below for more detailed steps
6+
7+
*NOTE:* The use of gradlew is required
8+
9+
1. Create standalone deployer zip
10+
```
11+
./gradlew makeOfflineZip
12+
```
13+
2. Copy zip (build/distributions/offline.zip) to desired location / server and unzip
14+
15+
3. Run disconnected tasks from unzip location
16+
```
17+
./gradlew mlDeploy -Pdisconnected=true
18+
```
19+
20+
21+
## Overview
22+
23+
An example of how to create a completely self-contained deployer zip that contains -
24+
1. All of the plugin / depencencies required to run the deployment (including ml-gradle)
25+
2. The gradle distribution
26+
27+
Once the zip has been created, you only need Java to run the deployment tasks
28+
29+
This approach is useful when you need to create a package that does not require any external ressources (e.g. maven/gradle repositories) to perform deployment operations.
30+
31+
## Requirements
32+
33+
* Java 8/9
34+
* Internet connection (for creation of zip only)
35+
36+
37+
## How it works
38+
39+
This project will -
40+
* Download all of the required dependencies (including plugins) into the 'build/offline/maven-repo' directory in the project.
41+
* Download the gradle binary distribution (zip) into the 'build/offline/gradle/wrapper' directory in the project
42+
* Create an offline.zip in build/distributions that contains
43+
* the gradle project itsself
44+
* all of the required depenencies
45+
* the gradle distribution that works with the gradlew executable
46+
47+
48+
## Usage
49+
50+
*NOTE:* It is important to use the gradlew executable as it will download the gradle distribution that will be encorporated into the self-contained deployer zip
51+
52+
## 1. Create the self-contained deployer zip
53+
54+
*NOTE:* This needs to be executed from a machine with access to the internet. It will create the zip at the location build/distributions/offline.zip
55+
56+
#### Linux / Mac
57+
58+
```
59+
./gradlew makeOfflineZip
60+
```
61+
62+
#### Windows
63+
64+
```
65+
gradlew makeOfflineZip
66+
```
67+
68+
69+
## 2. Unzip the distribution
70+
71+
Copy the created offline.zip to the desired location and unzip
72+
73+
```
74+
unzip offline.zip
75+
```
76+
77+
## 3. Execute disconnected the gradle tasks
78+
79+
From the directory that you have unzipped the offline.zip file into
80+
81+
```
82+
./gradlew mlDeploy -Pdisconnected=true
83+
```
84+
85+
This will use the jars that you have already downloaded to 'build/offline/maven-repo'
86+
87+
## Customise
88+
89+
**IMPORTANT**: If you want to include dependencies for a configuration (e.g. compile, runtime, mlcp etc), then you need to modify the 'downloadToProjectMavenRepo' task to include the relevant configuration. E.g. by adding 'configurations.compile.files' to the beginning of the task, all of the dependencies for the 'compile' task will be downloaded.
90+
91+
E.g. (assuming you are using the java plugin), the configuration below will download all the compile and runtime dependencies that you have defined
92+
93+
```
94+
task downloadToProjectMavenRepo(type: Copy) {
95+
configurations.compile.files
96+
configurations.runtime.files
97+
...
98+
```
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
plugins {
2+
id 'java' //optional - delete if not needed
3+
id 'net.saliman.properties' version '1.4.6'
4+
id 'com.marklogic.ml-gradle' version '3.7.1'
5+
}
6+
7+
repositories {
8+
// To use gradle in disconnected mode, you just need to set the 'disconnected' property. E.g. gradle compileJava -Pdisconnected
9+
if ( project.hasProperty("disconnected") && !"FALSE".equalsIgnoreCase(disconnected)) {
10+
println "Using offline repositories"
11+
maven {url uri( projectMavenRepo ) }
12+
} else {
13+
println "Using online repositories"
14+
jcenter()
15+
maven { url "https://developer.marklogic.com/maven2/" }
16+
}
17+
}
18+
19+
configurations {
20+
mlcp //example if you want to use mlcp. Delete otherwise
21+
}
22+
23+
dependencies {
24+
// sample java compile dependency. Remove if not required
25+
compile 'com.marklogic:marklogic-xcc:9.0.4'
26+
27+
//sample mlcp dependency. Remove if not required
28+
mlcp "com.marklogic:mlcp:9.0.5"
29+
}
30+
31+
32+
33+
/**
34+
* START: Disconnected gradle tasks
35+
*/
36+
gradle.taskGraph.whenReady { graph ->
37+
if (graph.hasTask(downloadToProjectMavenRepo)) {
38+
println project.gradle.gradleUserHomeDir
39+
if (!project.gradle.gradleUserHomeDir.equals(new File(rootDir,projectGradleHome))) {
40+
throw new GradleException("Please set the gradle user home property to $projectGradleHome on the gradle command line - e.g. \n " +
41+
(System.getProperty("os.name").startsWith("Windows") ? "" : "./") +
42+
"gradlew -Dgradle.user.home=$projectGradleHome <task_to_execute>")
43+
}
44+
}
45+
}
46+
47+
task downloadToProjectMavenRepo(type: Copy) {
48+
/*
49+
* Include any configuration dependencies here that you want to copy the dependencies for.
50+
* These are defined in the 'dependencies' block. E.g. you need to include
51+
* configurations.compile.files if you want your java 'compile' dependencies downloaded
52+
*/
53+
configurations.compile.files //includes 'java' compile dependencies. Remove if not needed
54+
configurations.mlcp.files //includes 'mlcp' dependencies. Remove if not needed
55+
56+
from new File(gradle.gradleUserHomeDir, 'caches/modules-2/files-2.1') // correct as of gradle 4.7
57+
into new File(rootDir, projectMavenRepo)
58+
eachFile {
59+
List<String> parts = it.path.split('/')
60+
it.path = (parts[0].replace('.','/') + '/' + parts[1]) + '/' + parts[2] + '/' + parts[4]
61+
}
62+
includeEmptyDirs false
63+
}
64+
65+
task makeOfflineZip(type: Zip, dependsOn: downloadToProjectMavenRepo) {
66+
from rootDir
67+
excludes = ['.tmp','.gradle','build/gradle-home','build/distributions','build/offline/gradle/wrapper/dists']
68+
destinationDir(file('build/distributions'))
69+
archiveName = 'offline.zip'
70+
doLast {
71+
println "Created offline project zip at build/distributions/offline.zip"
72+
}
73+
}
74+
75+
/**
76+
* END: Disconnected gradle tasks
77+
*/
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
projectMavenRepo=build/offline/maven-repo
2+
# if you change this, you need to modify gradlew and gradlew.bat also
3+
projectGradleHome=build/gradle-home
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
distributionBase=PROJECT
2+
distributionPath=build/offline/gradle/wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.8-bin.zip
4+
zipStoreBase=PROJECT
5+
zipStorePath=build/offline/gradle/wrapper
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
#!/usr/bin/env sh
2+
3+
##############################################################################
4+
##
5+
## Gradle start up script for UN*X
6+
##
7+
##############################################################################
8+
9+
# Attempt to set APP_HOME
10+
# Resolve links: $0 may be a link
11+
PRG="$0"
12+
# Need this for relative symlinks.
13+
while [ -h "$PRG" ] ; do
14+
ls=`ls -ld "$PRG"`
15+
link=`expr "$ls" : '.*-> \(.*\)$'`
16+
if expr "$link" : '/.*' > /dev/null; then
17+
PRG="$link"
18+
else
19+
PRG=`dirname "$PRG"`"/$link"
20+
fi
21+
done
22+
SAVED="`pwd`"
23+
cd "`dirname \"$PRG\"`/" >/dev/null
24+
APP_HOME="`pwd -P`"
25+
cd "$SAVED" >/dev/null
26+
27+
APP_NAME="Gradle"
28+
APP_BASE_NAME=`basename "$0"`
29+
# Set the gradle home to be a local project directorys
30+
GRADLE_USER_HOME="$APP_HOME/build/gradle-home"
31+
echo "Setting the GRADLE_USER_HOME to $GRADLE_USER_HOME \n(needed for the creation of the offline repository)\n"
32+
33+
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
34+
DEFAULT_JVM_OPTS=""
35+
36+
# Use the maximum available, or set MAX_FD != -1 to use that value.
37+
MAX_FD="maximum"
38+
39+
warn () {
40+
echo "$*"
41+
}
42+
43+
die () {
44+
echo
45+
echo "$*"
46+
echo
47+
exit 1
48+
}
49+
50+
# OS specific support (must be 'true' or 'false').
51+
cygwin=false
52+
msys=false
53+
darwin=false
54+
nonstop=false
55+
case "`uname`" in
56+
CYGWIN* )
57+
cygwin=true
58+
;;
59+
Darwin* )
60+
darwin=true
61+
;;
62+
MINGW* )
63+
msys=true
64+
;;
65+
NONSTOP* )
66+
nonstop=true
67+
;;
68+
esac
69+
70+
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
71+
72+
# Determine the Java command to use to start the JVM.
73+
if [ -n "$JAVA_HOME" ] ; then
74+
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
75+
# IBM's JDK on AIX uses strange locations for the executables
76+
JAVACMD="$JAVA_HOME/jre/sh/java"
77+
else
78+
JAVACMD="$JAVA_HOME/bin/java"
79+
fi
80+
if [ ! -x "$JAVACMD" ] ; then
81+
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
82+
83+
Please set the JAVA_HOME variable in your environment to match the
84+
location of your Java installation."
85+
fi
86+
else
87+
JAVACMD="java"
88+
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
89+
90+
Please set the JAVA_HOME variable in your environment to match the
91+
location of your Java installation."
92+
fi
93+
94+
# Increase the maximum file descriptors if we can.
95+
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
96+
MAX_FD_LIMIT=`ulimit -H -n`
97+
if [ $? -eq 0 ] ; then
98+
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
99+
MAX_FD="$MAX_FD_LIMIT"
100+
fi
101+
ulimit -n $MAX_FD
102+
if [ $? -ne 0 ] ; then
103+
warn "Could not set maximum file descriptor limit: $MAX_FD"
104+
fi
105+
else
106+
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
107+
fi
108+
fi
109+
110+
# For Darwin, add options to specify how the application appears in the dock
111+
if $darwin; then
112+
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
113+
fi
114+
115+
# For Cygwin, switch paths to Windows format before running java
116+
if $cygwin ; then
117+
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
118+
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
119+
JAVACMD=`cygpath --unix "$JAVACMD"`
120+
121+
# We build the pattern for arguments to be converted via cygpath
122+
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
123+
SEP=""
124+
for dir in $ROOTDIRSRAW ; do
125+
ROOTDIRS="$ROOTDIRS$SEP$dir"
126+
SEP="|"
127+
done
128+
OURCYGPATTERN="(^($ROOTDIRS))"
129+
# Add a user-defined pattern to the cygpath arguments
130+
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
131+
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
132+
fi
133+
# Now convert the arguments - kludge to limit ourselves to /bin/sh
134+
i=0
135+
for arg in "$@" ; do
136+
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
137+
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
138+
139+
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
140+
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
141+
else
142+
eval `echo args$i`="\"$arg\""
143+
fi
144+
i=$((i+1))
145+
done
146+
case $i in
147+
(0) set -- ;;
148+
(1) set -- "$args0" ;;
149+
(2) set -- "$args0" "$args1" ;;
150+
(3) set -- "$args0" "$args1" "$args2" ;;
151+
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
152+
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
153+
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
154+
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
155+
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
156+
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
157+
esac
158+
fi
159+
160+
# Escape application args
161+
save () {
162+
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
163+
echo " "
164+
}
165+
APP_ARGS=$(save "$@")
166+
167+
# Collect all arguments for the java command, following the shell quoting and substitution rules
168+
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" "\"-Dgradle.user.home=$GRADLE_USER_HOME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
169+
170+
# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
171+
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
172+
cd "$(dirname "$0")"
173+
fi
174+
175+
exec "$JAVACMD" "$@"

0 commit comments

Comments
 (0)