1 package org.apache.maven.plugins.jlink;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.lang.reflect.Method;
24 import java.util.Collection;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Optional;
28
29 import org.apache.maven.execution.MavenSession;
30 import org.apache.maven.plugin.AbstractMojo;
31 import org.apache.maven.plugins.annotations.Component;
32 import org.apache.maven.plugins.annotations.Parameter;
33 import org.apache.maven.project.MavenProject;
34 import org.apache.maven.toolchain.Toolchain;
35 import org.apache.maven.toolchain.ToolchainManager;
36
37
38
39
40 public abstract class AbstractJLinkMojo
41 extends AbstractMojo
42 {
43
44
45
46
47
48
49
50 @Parameter
51 private Map<String, String> jdkToolchain;
52
53 @Parameter( defaultValue = "${project}", readonly = true, required = true )
54 private MavenProject project;
55
56 @Parameter( defaultValue = "${session}", readonly = true, required = true )
57 private MavenSession session;
58
59 @Component
60 private ToolchainManager toolchainManager;
61
62
63
64
65
66 protected abstract String getClassifier();
67
68 protected JLinkExecutor getJlinkExecutor()
69 {
70 return new JLinkExecutor( getToolchain().orElse( null ), getLog() );
71 }
72
73 protected Optional<Toolchain> getToolchain()
74 {
75 Toolchain tc = null;
76
77 if ( jdkToolchain != null )
78 {
79
80 try
81 {
82 Method getToolchainsMethod = toolchainManager.getClass().getMethod( "getToolchains",
83 MavenSession.class, String.class, Map.class );
84
85 @SuppressWarnings( "unchecked" )
86 List<Toolchain> tcs = (List<Toolchain>) getToolchainsMethod.invoke( toolchainManager, getSession(),
87 "jdk", jdkToolchain );
88
89 if ( tcs != null && tcs.size() > 0 )
90 {
91 tc = tcs.get( 0 );
92 }
93 }
94 catch ( ReflectiveOperationException | SecurityException | IllegalArgumentException e )
95 {
96
97 }
98 }
99
100 if ( tc == null )
101 {
102
103 tc = toolchainManager.getToolchainFromBuildContext( "jdk", getSession() );
104 }
105
106 return Optional.ofNullable( tc );
107 }
108
109 protected MavenProject getProject()
110 {
111 return project;
112 }
113
114 protected MavenSession getSession()
115 {
116 return session;
117 }
118
119
120
121
122
123
124
125
126
127
128 protected File getArchiveFile( File basedir, String finalName, String classifier, String archiveExt )
129 {
130 if ( basedir == null )
131 {
132 throw new IllegalArgumentException( "basedir is not allowed to be null" );
133 }
134 if ( finalName == null )
135 {
136 throw new IllegalArgumentException( "finalName is not allowed to be null" );
137 }
138 if ( archiveExt == null )
139 {
140 throw new IllegalArgumentException( "archiveExt is not allowed to be null" );
141 }
142
143 if ( finalName.isEmpty() )
144 {
145 throw new IllegalArgumentException( "finalName is not allowed to be empty." );
146 }
147 if ( archiveExt.isEmpty() )
148 {
149 throw new IllegalArgumentException( "archiveExt is not allowed to be empty." );
150 }
151
152 StringBuilder fileName = new StringBuilder( finalName );
153
154 if ( hasClassifier( classifier ) )
155 {
156 fileName.append( "-" ).append( classifier );
157 }
158
159 fileName.append( '.' );
160 fileName.append( archiveExt );
161
162 return new File( basedir, fileName.toString() );
163 }
164
165 protected boolean hasClassifier( String classifier )
166 {
167 boolean result = false;
168 if ( classifier != null && !classifier.isEmpty() )
169 {
170 result = true;
171 }
172
173 return result;
174 }
175
176
177
178
179
180
181
182
183 protected StringBuilder convertSeparatedModulePathToPlatformSeparatedModulePath( String pluginModulePath )
184 {
185 StringBuilder sb = new StringBuilder();
186
187
188 String[] splitModule = pluginModulePath.split( "[;:]" );
189 for ( String module : splitModule )
190 {
191 if ( sb.length() > 0 )
192 {
193 sb.append( File.pathSeparatorChar );
194 }
195 sb.append( module );
196 }
197 return sb;
198 }
199
200
201
202
203
204
205
206 protected String getPlatformDependSeparateList( Collection<String> modulePaths )
207 {
208 return String.join( Character.toString( File.pathSeparatorChar ), modulePaths );
209 }
210
211
212
213
214
215
216 protected String getCommaSeparatedList( Collection<String> modules )
217 {
218 return String.join( ",", modules );
219 }
220
221 }