-
Notifications
You must be signed in to change notification settings - Fork 51
/
MetadataCopyMojo.java
234 lines (198 loc) · 10.9 KB
/
MetadataCopyMojo.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* Copyright (c) 2020, 2022 Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.graalvm.buildtools.maven;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.graalvm.buildtools.agent.StandardAgentMode;
import org.graalvm.buildtools.maven.config.AbstractMergeAgentFilesMojo;
import org.graalvm.buildtools.maven.config.agent.AgentConfiguration;
import org.graalvm.buildtools.maven.config.agent.MetadataCopyConfiguration;
import org.graalvm.buildtools.utils.NativeImageConfigurationUtils;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
import java.util.Objects;
import java.util.stream.Collectors;
@Mojo(name = "metadata-copy", defaultPhase = LifecyclePhase.PREPARE_PACKAGE)
public class MetadataCopyMojo extends AbstractMergeAgentFilesMojo {
private static final String DEFAULT_OUTPUT_DIRECTORY = "/META-INF/native-image";
private static final List<String> FILES_REQUIRED_FOR_MERGE = Arrays.asList("reflect-config.json", "jni-config.json", "proxy-config.json", "resource-config.json");
@Parameter(alias = "agent")
private AgentConfiguration agentConfiguration;
@Parameter(defaultValue = "${project}", readonly = true, required = true)
private MavenProject project;
@Parameter(defaultValue = "${session}", readonly = true)
private MavenSession session;
@Override
public void execute() throws MojoExecutionException {
if (agentConfiguration != null && (agentConfiguration.isEnabled() || agentIsEnabledFromCmd())) {
// in direct mode user is fully responsible for agent configuration, and we will not execute anything besides line that user provided
if (agentConfiguration.getDefaultMode().equalsIgnoreCase("direct")) {
logger.info("You are running agent in direct mode. Skipping both merge and metadata copy tasks.");
logger.info("In direct mode, user takes full responsibility for agent configuration.");
return;
}
MetadataCopyConfiguration config = agentConfiguration.getMetadataCopyConfiguration();
if (config == null) {
getLog().info("Metadata copy config not provided. Skipping this task.");
return;
}
String buildDirectory = project.getBuild().getDirectory() + "/native/agent-output/";
String destinationDir = config.getOutputDirectory();
if (destinationDir == null) {
destinationDir = project.getBuild().getOutputDirectory() + DEFAULT_OUTPUT_DIRECTORY;
}
if (!Files.isDirectory(Paths.get(destinationDir))) {
logger.warn("Destination directory " + destinationDir + " doesn't exist.");
logger.warn("Creating directory at: " + destinationDir);
boolean success = new File(destinationDir).mkdirs();
if (!success) {
throw new MojoExecutionException("Cannot create directory at the given location: " + destinationDir);
}
}
Path nativeImageExecutable = NativeImageConfigurationUtils.getNativeImage(logger);
tryInstallMergeExecutable(nativeImageExecutable);
executeCopy(buildDirectory, destinationDir);
getLog().info("Metadata copy process finished.");
}
}
private void executeCopy(String buildDirectory, String destinationDir) throws MojoExecutionException {
MetadataCopyConfiguration config = agentConfiguration.getMetadataCopyConfiguration();
List<String> sourceDirectories = getSourceDirectories(config.getDisabledStages(), buildDirectory);
// in case we have both main and test phase disabled, we don't need to copy anything
if (sourceDirectories.isEmpty()) {
logger.warn("Skipping metadata copy task. Both main and test stages are disabled in metadata copy configuration.");
return;
}
// In case user wants to merge agent-output files with some existing files in output directory, we need to check if there are some
// files in outputDirectory that can be merged. If the output directory is empty, we ignore user instruction to merge files.
if (config.shouldMerge() && !isDirectoryEmpty(destinationDir)) {
// If output directory contains some files, we need to check if the directory contains all necessary files for merge
if (!dirContainsFilesForMerge(destinationDir)) {
List<String> destinationDirContent = Arrays.stream(Objects.requireNonNull(new File(destinationDir).listFiles())).map(File::getName).collect(Collectors.toList());
List<String> missingFiles = getListDiff(FILES_REQUIRED_FOR_MERGE, destinationDirContent);
throw new MojoExecutionException("There are missing files for merge in output directory. If you want to merge agent files with " +
"existing files in output directory, please make sure that output directory contains all of the following files: " +
"reflect-config.json, jni-config.json, proxy-config.json, resource-config.json. Currently the output directory is " +
"missing: " + missingFiles);
}
sourceDirectories.add(destinationDir);
}
if (!checkIfSourcesExists(sourceDirectories)) {
return;
}
String sourceDirsInfo = sourceDirectories.stream().map(File::new).map(File::getName).collect(Collectors.joining(", "));
logger.info("Copying files from: " + sourceDirsInfo);
List<String> nativeImageConfigureOptions = new StandardAgentMode().getNativeImageConfigureOptions(sourceDirectories, Collections.singletonList(destinationDir));
nativeImageConfigureOptions.add(0, mergerExecutable.getAbsolutePath());
ProcessBuilder processBuilder = new ProcessBuilder(nativeImageConfigureOptions);
try {
Process start = processBuilder.start();
int retCode = start.waitFor();
if (retCode != 0) {
getLog().error("Metadata copy process failed with code: " + retCode);
throw new MojoExecutionException("Metadata copy process failed.");
}
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}
private boolean checkIfSourcesExists(List<String> sourceDirectories) {
for (String source : sourceDirectories) {
File dir = new File(source);
if (!dir.isDirectory() || !dir.exists()) {
logger.warn("Cannot find source directory " + source + " for metadata copy. Please check if you configured agent" +
" properly and it generates all necessary directories. If you want to skipp copy from some source, please " +
"configure metadataCopy with disable stage you want to skipp.");
return false;
}
}
return true;
}
private List<String> getSourceDirectories(List<String> disabledStages, String buildDirectory) {
List<String> sourceDirectories = new ArrayList<>();
sourceDirectories.add(buildDirectory + NativeExtension.Context.main);
sourceDirectories.add(buildDirectory + NativeExtension.Context.test);
for (String disabledStage : disabledStages) {
sourceDirectories.remove(buildDirectory + disabledStage);
}
return sourceDirectories;
}
private boolean isDirectoryEmpty(String dirName) {
File directory = new File(dirName);
File[] content = directory.listFiles();
return content == null || content.length == 0;
}
//check if we have all files needed for native-image-configure generate tool
private boolean dirContainsFilesForMerge(String dir) {
File baseDir = new File(dir);
File[] content = baseDir.listFiles();
if (content == null) {
return false;
}
List<String> dirContent = Arrays.stream(content).map(File::getName).collect(Collectors.toList());
return getListDiff(FILES_REQUIRED_FOR_MERGE, dirContent).isEmpty();
}
private List<String> getListDiff(List<String> list1, List<String> list2) {
List<String> diff = new ArrayList<>(list1);
diff.removeAll(list2);
return diff;
}
private boolean agentIsEnabledFromCmd() {
if (session == null) {
return false;
}
if (session.getSystemProperties().getProperty("agent") == null) {
return false;
}
return session.getSystemProperties().getProperty("agent").equalsIgnoreCase("true");
}
}