AutoconfigMojo.java
5.34 KB
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
package com.taobao.maven.plugin.autoconfig;
import java.io.File;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import com.taobao.logconfig.LogConfigurator;
import com.taobao.maven.plugin.config.ConfigRuntimeImpl;
import com.taobao.maven.plugin.expand.ExpanderRuntimeImpl;
import com.taobao.maven.plugin.util.CharsetUtil;
import com.taobao.maven.plugin.util.StringUtil;
/**
* Maven plugin to invoke antx-autoconfig.
*
* @goal autoconfig
* @phase package
*/
public class AutoconfigMojo extends AbstractMojo {
/**
* Package file or exploded dir to config.
*
* @parameter expression="${project.artifact.file}"
* @required
*/
private File dest;
/**
* exploding package to directory.
*
* @parameter
* expression="${project.build.directory}/${project.build.finalName}"
*/
private File explodedDirectory;
/**
* Whether or not to exploding package into directory.
*
* @parameter expression="${autoconfig.exploding}" default-value="false"
*/
private boolean exploding;
/**
* Charset encoding of console.
*
* @parameter expression="${autoconfig.charset}"
*/
private String charset;
/**
* Strict mode.
*
* @parameter expression="${autoconfig.strict}" default-value="true"
*/
private boolean strict;
/**
* Interactive mode switch.
*
* @parameter expression="${autoconfig.interactive}"
*/
private Boolean interactive;
/**
* Skipping autoconfig.
*
* @parameter expression="${autoconfig.skip}"
*/
private boolean skip;
/**
* Package type: war, jar, ear, etc.
*
* @parameter expression="${autoconfig.type}"
*/
private String type;
/**
* User properties file.
*
* @parameter expression="${autoconfig.userProperties}"
*/
private File userProperties;
/**
* Shared properties file.
*
* @parameter expression="${autoconfig.sharedProperties}"
*/
private String sharedProperties;
/**
* Shared properties name.
*
* @parameter expression="${autoconfig.sharedPropertiesName}"
*/
private String sharedPropertiesName;
public void execute() throws MojoExecutionException, MojoFailureException {
if (skip) {
return;
}
String interactiveMode;
if (interactive == null) {
interactiveMode = "auto";
} else if (interactive) {
interactiveMode = "on";
} else {
interactiveMode = "off";
}
if (dest.exists()) {
getLog().info(" dest:" + dest.getAbsolutePath());
if (charset == null) {
charset = CharsetUtil.detectedSystemCharset();
}
getLog().info("-------------------------------------------------");
getLog().info("Detected system charset encoding: " + charset);
getLog().info("If your can't read the following text, specify correct one like this: ");
getLog().info("");
getLog().info(" mvn -Dautoconfig.charset=yourcharset");
getLog().info("");
LogConfigurator.getConfigurator().configureDefault(false, charset);
ConfigRuntimeImpl runtimeImpl = new ConfigRuntimeImpl(System.in, System.out, System.err, charset);
runtimeImpl.setInteractiveMode(interactiveMode);
runtimeImpl.setDests(new String[] { dest.getAbsolutePath() });
runtimeImpl.setType(type);
if (userProperties != null) {
runtimeImpl.setUserPropertiesFile(userProperties.getAbsolutePath(), null);
}
if (sharedProperties != null) {
String[] sharedPropertiesFiles = StringUtil.split(sharedProperties, ",");
sharedPropertiesName = sharedPropertiesName == null ? "sharedAutoconfig" : sharedPropertiesName;
runtimeImpl.setSharedPropertiesFiles(sharedPropertiesFiles, sharedPropertiesName, charset);
}
getLog().info(
"Configuring " + dest.getAbsolutePath() + ", interactiveMode=" + interactiveMode + ", strict="
+ strict);
getLog().info("-------------------------------------------------");
try {
if (!runtimeImpl.start() && strict) {
throw new RuntimeException("undefined placeholders");
}
} catch (Exception e) {
runtimeImpl.error(e);
throw new MojoExecutionException("Autoconfig failed", e);
}
if (exploding && explodedDirectory != null) {
unpack(dest, explodedDirectory);
}
} else {
getLog().error("Dest directory or file for autoconfig does not exist: " + dest.getAbsolutePath());
}
}
public void unpack(File srcfile, File destdir) throws MojoExecutionException {
ExpanderRuntimeImpl expander = new ExpanderRuntimeImpl(System.in, System.out, System.err, charset);
expander.getExpander().setSrcfile(srcfile.getAbsolutePath());
expander.getExpander().setDestdir(destdir.getAbsolutePath());
expander.start();
}
}