1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.devzendo.xplp;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.List;
27 import java.util.Properties;
28 import java.util.Set;
29
30 import org.apache.maven.artifact.Artifact;
31 import org.apache.maven.plugin.AbstractMojo;
32 import org.codehaus.plexus.util.StringUtils;
33
34
35
36
37
38
39 public class UnixScriptLauncherCreator extends LauncherCreator {
40 private final String mOsOutputSubDirectoryName;
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 public UnixScriptLauncherCreator(final AbstractMojo mojo,
59 final File outputDirectory,
60 final String osOutputSubDirectoryName,
61 final String mainClassName,
62 final String applicationName,
63 final String libraryDirectory,
64 final Set<Artifact> transitiveArtifacts,
65 final Set<File> resourceDirectories,
66 final Properties parameterProperties,
67 final String[] systemProperties,
68 final String[] vmArguments,
69 final String[] narClassifierTypes) {
70 super(mojo, outputDirectory, mainClassName,
71 applicationName, libraryDirectory,
72 transitiveArtifacts, resourceDirectories, parameterProperties,
73 systemProperties, vmArguments, narClassifierTypes);
74 mOsOutputSubDirectoryName = osOutputSubDirectoryName;
75 }
76
77
78
79
80 @Override
81 public void createLauncher() throws IOException {
82 final File osOutputDir = new File(getOutputDirectory(), mOsOutputSubDirectoryName);
83 final File binDir = new File(osOutputDir, "bin");
84 final File libDir = new File(osOutputDir, "lib");
85 osOutputDir.mkdirs();
86 binDir.mkdirs();
87 libDir.mkdirs();
88 final boolean allDirsOK = osOutputDir.exists()
89 && binDir.exists() && libDir.exists();
90 if (!allDirsOK) {
91 throw new IOException("Could not create required directories under " + getOutputDirectory().getAbsolutePath());
92 }
93
94 final List<String> jvmArgs = new ArrayList<String>();
95 jvmArgs.addAll(systemPropertiesAsJVMArgs(getSystemProperties()));
96 jvmArgs.addAll(vmArgumentsAsJVMArgs(getVmArguments()));
97 final StringBuilder jvmArgsString = new StringBuilder();
98 for (final String jvmArg : jvmArgs) {
99 jvmArgsString.append(jvmArg);
100 jvmArgsString.append(' ');
101 }
102 getParameterProperties().put("xplp.linuxjvmargs", jvmArgsString.toString());
103 getParameterProperties().put("xplp.linuxclasspatharray", transitiveArtifactsAsClassPathArray(getTransitiveArtifacts()));
104
105 final File outputRunScript = new File(binDir, getApplicationName());
106 copyInterpolatedPluginResource("linux/launcher.sh", outputRunScript);
107 makeExecutable(outputRunScript);
108
109 copyTransitiveArtifacts(libDir);
110 }
111
112 private List<String> vmArgumentsAsJVMArgs(final String[] vmArguments) {
113 return Arrays.asList(vmArguments);
114 }
115
116 private List<String> systemPropertiesAsJVMArgs(final String[] systemProperties) {
117 final List<String> addDList = new ArrayList<String>();
118 for (final String sysProp : systemProperties) {
119 addDList.add("-D" + sysProp);
120 }
121 return addDList;
122 }
123
124 private String transitiveArtifactsAsClassPathArray(final Set<Artifact> transitiveArtifacts) {
125 final ArrayList<String> libsAsArtifacts = new ArrayList<String>();
126 final Set<String> transitiveArtifactFileNames = getTransitiveJarOrNarArtifactFileNames(transitiveArtifacts);
127 for (final String fileName : transitiveArtifactFileNames) {
128 libsAsArtifacts.add("$progdir/../lib/" + fileName);
129 }
130
131 return StringUtils.join(libsAsArtifacts.iterator(), ":");
132 }
133 }