Skip to content

Commit 72dda94

Browse files
committed
U decoupling utils from maven
1 parent 978e6ab commit 72dda94

File tree

6 files changed

+78
-94
lines changed

6 files changed

+78
-94
lines changed

src/main/java/io/github/fvarrui/javapackager/utils/CommandUtils.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import org.apache.commons.lang3.StringUtils;
99
import org.apache.commons.lang3.SystemUtils;
10-
import org.apache.maven.plugin.MojoExecutionException;
1110
import org.codehaus.plexus.util.cli.CommandLineException;
1211

1312
public class CommandUtils {
@@ -43,8 +42,7 @@ private static void createArguments(Commandline command, Object... arguments) {
4342
}
4443
}
4544

46-
public static String execute(File workingDirectory, String executable, Object... arguments)
47-
throws MojoExecutionException {
45+
public static String execute(File workingDirectory, String executable, Object... arguments) throws Exception {
4846
StringBuffer outputBuffer = new StringBuffer();
4947
StringBuffer errorBuffer = new StringBuffer();
5048
try {
@@ -86,13 +84,13 @@ public static String execute(File workingDirectory, String executable, Object...
8684
}
8785

8886
} catch (IOException | CommandLineException e) {
89-
throw new MojoExecutionException(e.getMessage(), e);
87+
throw new Exception(e.getMessage(), e);
9088
}
9189

9290
return outputBuffer.toString();
9391
}
9492

95-
public static String execute(String executable, Object... arguments) throws MojoExecutionException {
93+
public static String execute(String executable, Object... arguments) throws Exception {
9694
return execute(new File("."), executable, arguments);
9795
}
9896

src/main/java/io/github/fvarrui/javapackager/utils/FileUtils.java

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
import org.apache.commons.io.IOUtils;
2626
import org.apache.commons.lang3.StringUtils;
27-
import org.apache.maven.plugin.MojoExecutionException;
2827

2928

3029
public class FileUtils {
@@ -41,26 +40,26 @@ public static File mkdir(File parent, String name) {
4140
return mkdir(dir);
4241
}
4342

44-
public static void copyFileToFile(File source, File dest) throws MojoExecutionException {
43+
public static void copyFileToFile(File source, File dest) throws Exception {
4544
Logger.info("Copying file [" + source + "] to folder [" + dest + "]");
4645
try {
4746
copyFile(source, dest);
4847
} catch (IOException e) {
49-
throw new MojoExecutionException(e.getMessage(), e);
48+
throw new Exception(e.getMessage(), e);
5049
}
5150
}
5251

53-
public static void copyFileToFolder(File source, File destFolder) throws MojoExecutionException {
52+
public static void copyFileToFolder(File source, File destFolder) throws Exception {
5453
Logger.info("Copying file [" + source + "] to folder [" + destFolder + "]");
5554
if (new File(destFolder, source.getName()).exists()) return;
5655
try {
5756
copyFileToDirectory(source, destFolder);
5857
} catch (IOException e) {
59-
throw new MojoExecutionException(e.getMessage(), e);
58+
throw new Exception(e.getMessage(), e);
6059
}
6160
}
6261

63-
public static void concat(File dest, File ... sources) throws MojoExecutionException {
62+
public static void concat(File dest, File ... sources) throws Exception {
6463
Logger.info("Concatenating files [" + StringUtils.join(sources, ",") + "] into file [" + dest + "]");
6564
try {
6665
FileOutputStream fos = new FileOutputStream(dest);
@@ -72,25 +71,25 @@ public static void concat(File dest, File ... sources) throws MojoExecutionExcep
7271
fos.flush();
7372
fos.close();
7473
} catch (IOException e) {
75-
throw new MojoExecutionException("Error concatenating streams", e);
74+
throw new Exception("Error concatenating streams", e);
7675
}
7776
}
7877

79-
public static void copyFolderToFolder(File from, File to) throws MojoExecutionException {
78+
public static void copyFolderToFolder(File from, File to) throws Exception {
8079
Logger.info("Copying folder [" + from + "] to folder [" + to + "]");
81-
if (!from.isDirectory()) throw new MojoExecutionException("Source folder " + from + " is not a directory");
80+
if (!from.isDirectory()) throw new Exception("Source folder " + from + " is not a directory");
8281
try {
8382
copyDirectoryToDirectory(from, to);
8483
} catch (IOException e) {
85-
throw new MojoExecutionException(e.getMessage(), e);
84+
throw new Exception(e.getMessage(), e);
8685
}
8786
}
8887

89-
public static void copyFolderContentToFolder(File from, File to) throws MojoExecutionException {
88+
public static void copyFolderContentToFolder(File from, File to) throws Exception {
9089
Logger.info("Copying folder content [" + from + "] to folder [" + to + "]");
91-
if (!from.isDirectory()) throw new MojoExecutionException("Source folder " + from + " is not a directory");
90+
if (!from.isDirectory()) throw new Exception("Source folder " + from + " is not a directory");
9291
if (!to.exists()) to.mkdirs();
93-
else if (!to.isDirectory()) throw new MojoExecutionException("Destination folder " + to + " is not a directory");
92+
else if (!to.isDirectory()) throw new Exception("Destination folder " + to + " is not a directory");
9493
for (File file : from.listFiles()) {
9594
if (file.isDirectory())
9695
copyFolderToFolder(file, to);
@@ -99,21 +98,21 @@ public static void copyFolderContentToFolder(File from, File to) throws MojoExec
9998
}
10099
}
101100

102-
public static void moveFolderToFolder(File from, File to) throws MojoExecutionException {
101+
public static void moveFolderToFolder(File from, File to) throws Exception {
103102
Logger.info("Moving folder [" + from + "] to folder [" + to + "]");
104-
if (!from.isDirectory()) throw new MojoExecutionException("Source folder " + from + " is not a directory");
105-
else if (to.exists() && !to.isDirectory()) throw new MojoExecutionException("Destination folder " + to + " is not a directory");
103+
if (!from.isDirectory()) throw new Exception("Source folder " + from + " is not a directory");
104+
else if (to.exists() && !to.isDirectory()) throw new Exception("Destination folder " + to + " is not a directory");
106105
try {
107106
moveDirectoryToDirectory(from, to, true);
108107
} catch (IOException e) {
109-
throw new MojoExecutionException(e.getMessage(), e);
108+
throw new Exception(e.getMessage(), e);
110109
}
111110
}
112111

113-
public static void moveFolderContentToFolder(File from, File to) throws MojoExecutionException {
112+
public static void moveFolderContentToFolder(File from, File to) throws Exception {
114113
Logger.info("Moving folder content [" + from + "] to folder [" + to + "]");
115-
if (!from.isDirectory()) throw new MojoExecutionException("Source folder " + from + " is not a directory");
116-
else if (!to.isDirectory()) throw new MojoExecutionException("Destination folder " + to + " is not a directory");
114+
if (!from.isDirectory()) throw new Exception("Source folder " + from + " is not a directory");
115+
else if (!to.isDirectory()) throw new Exception("Destination folder " + to + " is not a directory");
117116
try {
118117
for (File file : from.listFiles()) {
119118
if (file.isDirectory())
@@ -122,36 +121,36 @@ public static void moveFolderContentToFolder(File from, File to) throws MojoExec
122121
moveFileToDirectory(file, to, true);
123122
}
124123
} catch (IOException e) {
125-
throw new MojoExecutionException(e.getMessage(), e);
124+
throw new Exception(e.getMessage(), e);
126125
}
127126
}
128127

129-
public static void moveFileToFolder(File from, File to) throws MojoExecutionException {
128+
public static void moveFileToFolder(File from, File to) throws Exception {
130129
Logger.info("Moving file [" + from + "] to folder [" + to + "]");
131-
if (!from.isFile()) throw new MojoExecutionException("Source file " + from + " is not a file");
130+
if (!from.isFile()) throw new Exception("Source file " + from + " is not a file");
132131
if (!to.exists()) to.mkdirs();
133132
try {
134133
moveFileToDirectory(from, to, true);
135134
} catch (IOException e) {
136-
throw new MojoExecutionException(e.getMessage(), e);
135+
throw new Exception(e.getMessage(), e);
137136
}
138137
}
139138

140-
private static void copyStreamToFile(InputStream is, File dest) throws MojoExecutionException {
139+
private static void copyStreamToFile(InputStream is, File dest) throws Exception {
141140
try {
142141
copyInputStreamToFile(is, dest);
143142
} catch (IOException ex) {
144-
throw new MojoExecutionException("Could not copy input stream to " + dest, ex);
143+
throw new Exception("Could not copy input stream to " + dest, ex);
145144
}
146145
}
147146

148-
public static void copyResourceToFile(String resource, File dest, boolean unixStyleNewLines) throws MojoExecutionException {
147+
public static void copyResourceToFile(String resource, File dest, boolean unixStyleNewLines) throws Exception {
149148
copyResourceToFile(resource, dest);
150149
if (unixStyleNewLines) {
151150
try {
152151
processFileContent(dest, c -> c.replaceAll("\\r\\n", "\n").replaceAll("\\r", "\n"));
153152
} catch (IOException e) {
154-
throw new MojoExecutionException(e.getMessage(), e);
153+
throw new Exception(e.getMessage(), e);
155154
}
156155
}
157156
}
@@ -162,27 +161,27 @@ public static void processFileContent(File dest, Function<String, String> functi
162161
org.apache.commons.io.FileUtils.writeStringToFile(dest, content, Charset.forName("UTF-8"));
163162
}
164163

165-
public static void copyResourceToFile(String resource, File dest) throws MojoExecutionException {
164+
public static void copyResourceToFile(String resource, File dest) throws Exception {
166165
Logger.info("Copying resource [" + resource + "] to file [" + dest + "]");
167166
copyStreamToFile(FileUtils.class.getResourceAsStream(resource), dest);
168167
}
169168

170169

171-
public static void createSymlink(File link, File target) throws MojoExecutionException {
170+
public static void createSymlink(File link, File target) throws Exception {
172171
Logger.info("Creating symbolic link [" + link + "] to [" + target + "]");
173172
try {
174173
Files.createSymbolicLink(link.toPath(), target.toPath());
175174
} catch (IOException e) {
176-
throw new MojoExecutionException("Could not create symlink " + link + " to " + target, e);
175+
throw new Exception("Could not create symlink " + link + " to " + target, e);
177176
}
178177
}
179178

180-
public static void removeFolder(File folder) throws MojoExecutionException {
179+
public static void removeFolder(File folder) throws Exception {
181180
Logger.info("Removing folder [" + folder + "]");
182181
try {
183182
deleteDirectory(folder);
184183
} catch (IOException e) {
185-
throw new MojoExecutionException("Could not remove folder " + folder, e);
184+
throw new Exception("Could not remove folder " + folder, e);
186185
}
187186
}
188187

src/main/java/io/github/fvarrui/javapackager/utils/JDKUtils.java

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,44 +19,39 @@ public class JDKUtils {
1919
* @throws FileNotFoundException release file not found
2020
* @throws IOException release file could not be read
2121
*/
22-
public static Map<String, String> getRelease(File jdkPath) throws FileNotFoundException, IOException {
22+
private static Map<String, String> getRelease(File jdkPath) throws FileNotFoundException, IOException {
2323
Map<String, String> propertiesMap = new HashMap<>();
24-
2524
File releaseFile = new File(jdkPath, "release");
26-
27-
if (!releaseFile.exists()) {
28-
releaseFile = new File(jdkPath, "Contents/Home/release");
29-
}
30-
3125
if (!releaseFile.exists()) {
3226
throw new FileNotFoundException("release file not found");
3327
}
34-
3528
Properties properties = new Properties();
3629
properties.load(new FileInputStream(releaseFile));
3730
properties.forEach((key, value) -> propertiesMap.put(key.toString(), value.toString().replaceAll("^\"|\"$", "")));
38-
3931
return propertiesMap;
4032
}
4133

42-
public static boolean isValidJdk(Platform platform, File jdkPath) throws Exception {
43-
34+
private static boolean checkPlatform(Platform platform, File jdkPath) throws FileNotFoundException, IOException {
35+
Map<String, String> releaseMap = getRelease(jdkPath);
36+
String osName = releaseMap.get("OS_NAME");
37+
switch (platform) {
38+
case linux: return "Linux".equalsIgnoreCase(osName);
39+
case mac: return "Darwin".equalsIgnoreCase(osName);
40+
case windows: return "Windows".equalsIgnoreCase(osName);
41+
default: return false;
42+
}
43+
}
44+
45+
public static boolean isValidJDK(Platform platform, File jdkPath) throws FileNotFoundException, IOException {
46+
return checkPlatform(platform, jdkPath);
47+
}
48+
49+
public static boolean isValidJRE(Platform platform, File jrePath) throws IOException {
4450
try {
45-
Map<String, String> releaseMap = getRelease(jdkPath);
46-
String osName = releaseMap.get("OS_NAME");
47-
switch (platform) {
48-
case linux: return "Linux".equalsIgnoreCase(osName);
49-
case mac: return "Darwin".equalsIgnoreCase(osName);
50-
case windows: return "Windows".equalsIgnoreCase(osName);
51-
default:
52-
}
51+
return checkPlatform(platform, jrePath);
5352
} catch (FileNotFoundException e) {
54-
throw new Exception(e.getMessage(), e);
55-
} catch (IOException e) {
56-
throw new Exception(e.getMessage(), e);
53+
return new File(jrePath, "bin/java").exists() || new File(jrePath, "bin/java.exe").exists();
5754
}
58-
59-
return false;
6055
}
6156

6257
}

src/main/java/io/github/fvarrui/javapackager/utils/Logger.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
package io.github.fvarrui.javapackager.utils;
22

33
import org.apache.commons.lang3.StringUtils;
4-
import org.apache.maven.plugin.logging.Log;
4+
5+
import io.github.fvarrui.javapackager.gradle.GradleContext;
6+
import io.github.fvarrui.javapackager.maven.MavenContext;
57

68
public class Logger {
79

810
private static final String TAB = " ";
911

1012
private static int tabs = 0;
11-
private static Log logger;
12-
13-
public static void init(Log logger) {
14-
Logger.logger = logger;
15-
}
1613

1714
public static String error(String error) {
18-
if (logger != null) logger.error(StringUtils.repeat(TAB, tabs) + error);
15+
if (MavenContext.getLogger() != null) MavenContext.getLogger().error(StringUtils.repeat(TAB, tabs) + error);
16+
if (GradleContext.getLogger() != null) GradleContext.getLogger().error(StringUtils.repeat(TAB, tabs) + error);
1917
return error;
2018
}
2119

2220
public static String warn(String warn) {
23-
if (logger != null) logger.warn(StringUtils.repeat(TAB, tabs) + warn);
21+
if (MavenContext.getLogger() != null) MavenContext.getLogger().warn(StringUtils.repeat(TAB, tabs) + warn);
22+
if (GradleContext.getLogger() != null) GradleContext.getLogger().warn(StringUtils.repeat(TAB, tabs) + warn);
2423
return warn;
2524
}
2625

2726
public static String info(String info) {
28-
if (logger != null) logger.info(StringUtils.repeat(TAB, tabs) + info);
27+
if (MavenContext.getLogger() != null) MavenContext.getLogger().info(StringUtils.repeat(TAB, tabs) + info);
28+
if (GradleContext.getLogger() != null) GradleContext.getLogger().info(StringUtils.repeat(TAB, tabs) + info);
2929
return info;
3030
}
3131

src/main/java/io/github/fvarrui/javapackager/utils/VelocityUtils.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import java.util.UUID;
99

1010
import org.apache.commons.lang3.StringUtils;
11-
import org.apache.maven.plugin.MojoExecutionException;
1211
import org.apache.velocity.Template;
1312
import org.apache.velocity.VelocityContext;
1413
import org.apache.velocity.app.VelocityEngine;
@@ -45,7 +44,7 @@ private static VelocityEngine getVelocityEngine() {
4544
return velocityEngine;
4645
}
4746

48-
private static String render(String templatePath, Object info) throws MojoExecutionException {
47+
private static String render(String templatePath, Object info) throws Exception {
4948
VelocityContext context = new VelocityContext();
5049
context.put("features", new ArrayList<String>());
5150
context.put("GUID", UUID.class);
@@ -61,13 +60,13 @@ public static void setAssetsDir(File assetsDir) {
6160
VelocityUtils.assetsDir = assetsDir;
6261
}
6362

64-
public static void render(String templatePath, File output, Object info) throws MojoExecutionException {
63+
public static void render(String templatePath, File output, Object info) throws Exception {
6564
try {
6665
String data = render(templatePath, info);
6766
data = data.replaceAll("\\r\\n", "\n").replaceAll("\\r", "\n");
6867
writeStringToFile(output, data, "UTF-8");
6968
} catch (IOException e) {
70-
throw new MojoExecutionException(e.getMessage(), e);
69+
throw new Exception(e.getMessage(), e);
7170
}
7271
}
7372

0 commit comments

Comments
 (0)