-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathexec-artifact
executable file
·138 lines (120 loc) · 3.33 KB
/
exec-artifact
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/bin/bash
#
# Copyright 2014,2015 Evernote Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# Executes a Java class in a Maven artifact, automatically resolving all dependencies
#
# usage: exec-artifact [OPTIONS] <artifactId:groupId:version> <mainClass> [<arg> ...]
# exec-artifact --help
err=0
mavenOpts=""
while [ 1 ]; do
o="$1"
if [ -z "$o" -o "${o:0:1}" != "-" ]; then
break
fi
shift
case "$o" in
-h | --help )
err=1
;;
-o | -U | -q | -X )
mavenOpts+=" $o"
;;
*) echo "Illegal option: $o" >&2
exit 4
;;
esac
done
artifact="$1"
mainClass="$2"
if [ $# -lt 2 ]; then
err=1
elif [ -z "$artifact" -o -z "$mainClass" ]; then
err=2
else
echo "$artifact" | grep -c -q "[<>]"
if [ $? -eq 0 ]; then
err=3
fi
fi
function html() {
echo "$*" | sed 's/&/\&/g; s/</\</g; s/>/\>/g; s/"/\"/g; s/'"'"'/\'/g'
}
mainClass="$(html $mainClass)"
if [ "$err" -ne 0 ]; then
cat <<EOT
Syntax: $(basename $0) [OPTIONS] <artifactId:groupId:version> <mainClass> [<arg> ...]
Executes a Java class in a Maven artifact, automatically resolving all dependencies
OPTIONS:
-h, --help Show this help
-U Forces a check for updated snapshots on remote repositories
-q Quiet output - only show errors
-X Produce execution debug output
Examples:
$(basename $0) com.evernote.iwana:iwana-extract:1.0-SNAPSHOT com.evernote.iwana.extract.ExtractTextApp
EOT
exit $err
fi
shift 2
args=""
for a in "$@"; do
arg="$(html $a)"
args+="<argument>$arg</argument>"
done
read groupId artifactId version <<< $(echo "$artifact" | tr ":" " ")
f=$(mktemp /tmp/exec-artifact.XXXXXX)
cat >$f <<EOT
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.evernote.ai.tools</groupId>
<artifactId>exec-artifact</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>$groupId</groupId>
<artifactId>$artifactId</artifactId>
<version>$version</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<mainClass>$mainClass</mainClass>
<arguments>
$args
</arguments>
</configuration>
</plugin>
</plugins>
</build>
</project>
EOT
mvn=$(which mvn)
if [ -z "$mvn" ]; then
mvn=$(which mvn3)
fi
if [ -z "$mvn" ]; then
echo "Please install Maven, and add it to your PATH (so we can execute mvn or mvn3)" >&2
exit 10
fi
"$mvn" -q $mavenOpts -f "$f" exec:java
rm -f "$f"