This repository has been archived by the owner on Jun 7, 2021. It is now read-only.
/
Main.java
192 lines (156 loc) · 6.62 KB
/
Main.java
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
* Copyright 2015 Red Hat, Inc, and individual contributors.
*
* 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.
*/
package org.wildfly.swarm.swarmtool;
import joptsimple.OptionException;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;
import static java.util.Arrays.asList;
public class Main {
public static void main(final String [] args) throws Exception {
try {
generateSwarmJar(args);
} catch (ExitException e) {
final String msg = e.getMessage();
if (msg != null) {
System.err.println(msg);
}
System.exit(e.status);
}
}
protected static File generateSwarmJar(final String [] args) throws Exception {
OptionSet foundOptions = null;
try {
foundOptions = OPT_PARSER.parse(args);
} catch (OptionException e) {
System.err.println(e.getMessage() + "\n");
}
if (foundOptions == null ||
foundOptions.has(HELP_OPT)) {
OPT_PARSER.printHelpOn(System.err);
exit(null);
}
if (foundOptions.has(VERSION_OPT)) {
exit("swarmtool v" + VERSION, 0);
}
final List<File> nonOptArgs = foundOptions.valuesOf(SOURCE_OPT);
if (nonOptArgs.isEmpty()) {
exit("No source artifact specified.");
} if (nonOptArgs.size() > 1) {
exit("Too many source artifacts provided (" + nonOptArgs + ")");
}
final File source = nonOptArgs.get(0);
if (!source.exists()) {
exit("File " + source.getAbsolutePath() + " does not exist.");
}
final Properties properties = new Properties();
if (foundOptions.has(SYSPROPS_FILE_OPT)) {
try (InputStream in = new FileInputStream(foundOptions.valueOf(SYSPROPS_FILE_OPT))) {
properties.load(in);
}
}
foundOptions.valuesOf(SYSPROPS_OPT)
.forEach(prop -> {
final String[] parts = prop.split("=");
properties.put(parts[0], parts[1]);
});
return new Build()
.source(source)
.swarmVersion(VERSION)
.addSwarmDependencies(foundOptions.valuesOf(FRACTIONS_OPT))
.outputDir(new File(foundOptions.valueOf(OUTPUT_DIR_OPT)))
.name(foundOptions.valueOf(NAME_OPT))
.autoDetectFractions(!foundOptions.has(DISABLE_AUTO_DETECT))
.contextPath(foundOptions.valueOf(CONTEXT_PATH_OPT))
.properties(properties)
.run();
}
private static void exit(String message) {
exit(message, 1);
}
private static void exit(String message, int code) {
throw new ExitException(code, message);
}
private static final OptionParser OPT_PARSER = new OptionParser();
private static final OptionSpec<Void> HELP_OPT =
OPT_PARSER.acceptsAll(asList("h", "help"), "print help and exit")
.forHelp();
private static final OptionSpec<Void> VERSION_OPT =
OPT_PARSER.acceptsAll(asList("v", "version"), "print version and exit")
.forHelp();
private static final OptionSpec<Void> DISABLE_AUTO_DETECT =
OPT_PARSER.accepts("no-fraction-detect", "disable auto fraction detection");
private static final OptionSpec<String> FRACTIONS_OPT =
OPT_PARSER.acceptsAll(asList("f", "fractions"), "swarm fractions to include")
.withRequiredArg()
.ofType(String.class)
.withValuesSeparatedBy(',')
.describedAs("undertow,jaxrs,...");
private static final OptionSpec<String> OUTPUT_DIR_OPT =
OPT_PARSER.acceptsAll(asList("o", "output-dir"), "directory where the final jar will be written")
.withRequiredArg()
.ofType(String.class)
.defaultsTo(".")
.describedAs("path");
private static final OptionSpec<String> NAME_OPT =
OPT_PARSER.acceptsAll(asList("n", "name"), "The name of the final jar sans the -swarm.jar suffix (default: <source name>)")
.withRequiredArg()
.ofType(String.class)
.describedAs("jar-name");
private static final OptionSpec<File> SOURCE_OPT =
OPT_PARSER.nonOptions("The source artifact")
.ofType(File.class);
private static final OptionSpec<String> CONTEXT_PATH_OPT =
OPT_PARSER.acceptsAll(asList("p", "context-path"), "context path")
.withRequiredArg()
.ofType(String.class)
.defaultsTo("/")
.describedAs("context");
private static final OptionSpec<String> SYSPROPS_OPT =
OPT_PARSER.accepts("D", "system property (overrides entry in --property-file)")
.withRequiredArg()
.ofType(String.class)
.describedAs("key=value");
private static final OptionSpec<File> SYSPROPS_FILE_OPT =
OPT_PARSER.accepts("property-file", "system properties")
.withRequiredArg()
.ofType(File.class)
.describedAs("system properties file");
private static final String VERSION;
static {
Properties props = new Properties();
try (InputStream propStream = Main.class.getClassLoader()
.getResourceAsStream("org/wildfly/swarm/swarmtool/version.properties")) {
props.load(propStream);
} catch (IOException e) {
e.printStackTrace();
}
VERSION = props.getProperty("version");
}
static class ExitException extends RuntimeException {
ExitException(final int status, final String message) {
super(message);
this.status = status;
}
public int status;
}
}