Skip to content

Commit 06233b0

Browse files
authored
Merge pull request #1962 from turkeylurkey/issue-1959
Copy config and changed file to temp dir for feature generation
2 parents 2c6d870 + c4e9526 commit 06233b0

File tree

3 files changed

+51
-22
lines changed

3 files changed

+51
-22
lines changed

liberty-maven-plugin/src/main/java/io/openliberty/tools/maven/server/DevMojo.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* (C) Copyright IBM Corporation 2019, 2025.
2+
* (C) Copyright IBM Corporation 2019, 2026
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -446,7 +446,7 @@ public void libertyCreate() throws PluginExecutionException {
446446
}
447447

448448
@Override
449-
public boolean libertyGenerateFeatures(Collection<String> classes, boolean optimize, boolean useTmpDir) {
449+
public boolean libertyGenerateFeatures(Collection<String> classes, boolean optimize, boolean useTmpDirOut, boolean useTmpDirIn) {
450450
try {
451451
if (classes != null) {
452452
Element[] classesElem = new Element[classes.size()];
@@ -456,10 +456,10 @@ public boolean libertyGenerateFeatures(Collection<String> classes, boolean optim
456456
i++;
457457
}
458458
// generate features for only the classFiles passed
459-
runLibertyMojoGenerateFeatures(element(name("classFiles"), classesElem), optimize, useTmpDir);
459+
runLibertyMojoGenerateFeatures(element(name("classFiles"), classesElem), optimize, useTmpDirOut, useTmpDirIn);
460460
} else {
461461
// pass null for classFiles so that features are generated for ALL of the classes
462-
runLibertyMojoGenerateFeatures(null, optimize, useTmpDir);
462+
runLibertyMojoGenerateFeatures(null, optimize, useTmpDirOut, useTmpDirIn);
463463
}
464464
return true; // successfully generated features
465465
} catch (MojoExecutionException e) {
@@ -778,7 +778,7 @@ public boolean updateArtifactPaths(ProjectModule projectModule, boolean redeploy
778778
getLog().debug("Detected a change in the compile dependencies for "
779779
+ buildFile + " , regenerating features");
780780
// If generateToSrc is false then we must copy new generated features file from temp dir to server dir after install
781-
boolean generateFeaturesSuccess = libertyGenerateFeatures(null, true, !generateToSrc);
781+
boolean generateFeaturesSuccess = libertyGenerateFeatures(null, true, !generateToSrc, false);
782782
if (generateFeaturesSuccess) {
783783
util.getJavaSourceClassPaths().clear();
784784
}
@@ -1107,7 +1107,7 @@ public boolean recompileBuildFile(File buildFile, Set<String> compileArtifactPat
11071107
getLog().debug("Detected a change in the compile dependencies, regenerating features");
11081108
// always optimize generate features on dependency change
11091109
// If generateToSrc is false then we must copy new generated features file from temp dir to server dir after install
1110-
generateFeaturesSuccess = libertyGenerateFeatures(null, true, !generateToSrc);
1110+
generateFeaturesSuccess = libertyGenerateFeatures(null, true, !generateToSrc, false);
11111111
if (generateFeaturesSuccess) {
11121112
util.getJavaSourceClassPaths().clear();
11131113
} else {
@@ -1693,8 +1693,9 @@ private void generateFeaturesOnStartup() throws MojoExecutionException {
16931693
getLog().warn(
16941694
"The source configuration directory will be modified. Features will automatically be generated in a new file: "
16951695
+ generatedFileCanonicalPath);
1696-
// Only generate to a tmp dir once dev mode has started.
1697-
runLibertyMojoGenerateFeatures(null, true, false);
1696+
// During dev mode start up the server is not running yet so we will generate features to the correct
1697+
// output directory and then install features in the next step.
1698+
runLibertyMojoGenerateFeatures(null, true, false, false);
16981699
} catch (MojoExecutionException e) {
16991700
if (e.getCause() != null && e.getCause() instanceof PluginExecutionException) {
17001701
// PluginExecutionException indicates that the binary scanner jar could not be found
@@ -2191,7 +2192,7 @@ protected void runLibertyMojoCreate() throws MojoExecutionException {
21912192
* @throws MojoExecutionException
21922193
*/
21932194
@Override
2194-
protected void runLibertyMojoGenerateFeatures(Element classFiles, boolean optimize, boolean useTmpDir) throws MojoExecutionException {
2195-
super.runLibertyMojoGenerateFeatures(classFiles, optimize, useTmpDir);
2195+
protected void runLibertyMojoGenerateFeatures(Element classFiles, boolean optimize, boolean useTmpDirOut, boolean useTmpDirIn) throws MojoExecutionException {
2196+
super.runLibertyMojoGenerateFeatures(classFiles, optimize, useTmpDirOut, useTmpDirIn);
21962197
}
21972198
}

liberty-maven-plugin/src/main/java/io/openliberty/tools/maven/server/GenerateFeaturesMojo.java

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* (C) Copyright IBM Corporation 2021, 2025
2+
* (C) Copyright IBM Corporation 2021, 2026
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
55
* use this file except in compliance with the License. You may obtain a copy of
@@ -82,13 +82,28 @@ public class GenerateFeaturesMojo extends PluginConfigSupport {
8282
private boolean generateToSrc;
8383

8484
/**
85-
* The useDevModeTempDir parameter is for internal use only. It is not for users.
85+
* The useTempDirAsOutput parameter is for internal use only. It is not for users.
8686
* The parameter is only used when generateToSrc is false meaning we generate to serverDir.
87-
* When the parameter is true we will write the generated features file to the temp directory.
88-
* This is required because the features must all be installed before writing to serverDir in devmode.
87+
* It is needed in dev mode because the server is running and we need to ensure the features
88+
* that are generated are installed before we update a running server.
89+
* When the parameter is true we will write the generated features file to the special generate-features
90+
* temp directory.
8991
*/
90-
@Parameter(property = "useDevModeTempDir", defaultValue = "false")
91-
private boolean useDevModeTempDir;
92+
@Parameter(property = "useTempDirAsOutput", defaultValue = "false")
93+
private boolean useTempDirAsOutput;
94+
95+
/**
96+
* The useTempDirAsContext parameter is for internal use only. It is not for users.
97+
* It is needed in dev mode when the user updates a server config file which might affect
98+
* the features that will be generated. In such a case we will required the caller to copy the
99+
* serverDir configuration files into the special generate-features temp directory and augment it
100+
* with the file changed by the user. We do not do this all the time because of the performance
101+
* cost of copying all the files.
102+
* When the parameter is true we will use the generate-features temp directory as the context for
103+
* feature generation.
104+
*/
105+
@Parameter(property = "useTempDirAsContext", defaultValue = "false")
106+
private boolean useTempDirAsContext;
92107

93108
/**
94109
* Generating features is performed relative to a certain server. We only generate features
@@ -195,19 +210,26 @@ private void generateFeatures() throws MojoExecutionException, PluginExecutionEx
195210
}
196211
}
197212

198-
// The config dir is in the src directory. Otherwise generate for the target/liberty dir.
199-
generationContextDir = generateToSrc ? configDirectory : serverDirectory;
213+
if (useTempDirAsContext) {
214+
// When this parameter is true it is required that the caller has copied the config into this dir.
215+
generationContextDir = getGeneratedFeaturesTempDir();
216+
} else {
217+
// The config dir is the one in the src directory. Otherwise generate for the target/liberty/wlp dir.
218+
generationContextDir = generateToSrc ? configDirectory : serverDirectory;
219+
}
200220
// When using dev mode we always generate to a temporary directory so we can call install before writing to server dir.
201-
generationOutputDir = useDevModeTempDir ? new File(project.getBuild().getDirectory(), GENERATED_FEATURES_TEMP_DIR) : generationContextDir;
221+
generationOutputDir = useTempDirAsOutput ? getGeneratedFeaturesTempDir() : generationContextDir;
202222

203223
binaryScanner = getBinaryScannerJarFromRepository();
204224
BinaryScannerHandler binaryScannerHandler = new BinaryScannerHandler(binaryScanner);
205225

206226
getLog().debug("--- Generate Features values ---");
207227
getLog().debug("Binary scanner jar: " + binaryScanner.getName());
208228
getLog().debug("optimize generate features: " + optimize);
209-
getLog().debug("called by dev mode, useDevModeTempDir: " + useDevModeTempDir);
229+
getLog().debug("useTempDirAsOutput (dev mode only): " + useTempDirAsOutput);
230+
getLog().debug("useTempDirAsContext (dev mode only): " + useTempDirAsContext);
210231
getLog().debug("generate to directory: " + generationOutputDir.getAbsolutePath());
232+
211233
if (classFiles != null && !classFiles.isEmpty()) {
212234
getLog().debug("Generate features for the following class files: " + classFiles.toString());
213235
}
@@ -395,6 +417,11 @@ private Set<String> getGeneratedFeatures(ServerFeatureUtil servUtil, File genera
395417
return features;
396418
}
397419

420+
// returns the hidden directory we use for generate-features special purposes
421+
private File getGeneratedFeaturesTempDir() {
422+
return new File(project.getBuild().getDirectory(), GENERATED_FEATURES_TEMP_DIR);
423+
}
424+
398425
/**
399426
* Gets the binary scanner jar file from the local cache.
400427
* Downloads it first from connected repositories such as Maven Central if a newer release is available than the cached version.

liberty-maven-plugin/src/main/java/io/openliberty/tools/maven/server/StartDebugMojoSupport.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,13 +291,14 @@ protected void runLibertyMojoInstallFeature(Element features, File serverDir, St
291291
runLibertyMojo("install-feature", config);
292292
}
293293

294-
protected void runLibertyMojoGenerateFeatures(Element classFiles, boolean optimize, boolean useTmpDir) throws MojoExecutionException {
294+
protected void runLibertyMojoGenerateFeatures(Element classFiles, boolean optimize, boolean useTmpDirOut, boolean useTmpDirIn) throws MojoExecutionException {
295295
Xpp3Dom config = ExecuteMojoUtil.getPluginGoalConfig(getLibertyPlugin(), "generate-features", getLog());
296296
if (classFiles != null) {
297297
config = Xpp3Dom.mergeXpp3Dom(configuration(classFiles), config);
298298
}
299299
config.addChild(element(name("optimize"), Boolean.toString(optimize)).toDom());
300-
config.addChild(element(name("useDevModeTempDir"), Boolean.toString(useTmpDir)).toDom());
300+
config.addChild(element(name("useTempDirAsOutput"), Boolean.toString(useTmpDirOut)).toDom());
301+
config.addChild(element(name("useTempDirAsContext"), Boolean.toString(useTmpDirIn)).toDom());
301302
runLibertyMojo("generate-features", config);
302303
}
303304

0 commit comments

Comments
 (0)