From be3bdc537239b9784c0b7ade362eea605ccaea9b Mon Sep 17 00:00:00 2001 From: Stepan Miakchilo Date: Mon, 20 Nov 2023 23:54:06 +0300 Subject: [PATCH 1/7] [EAK-497] Added support for Maven toolchains --- plugin/pom.xml | 4 + .../aem/toolkit/plugin/maven/PluginMojo.java | 126 +++++++++++++++--- .../toolkit/plugin/utils/DialogConstants.java | 1 + .../toolkit/plugin/utils/ToolchainUtil.java | 72 ++++++++++ pom.xml | 6 + 5 files changed, 190 insertions(+), 19 deletions(-) create mode 100644 plugin/src/main/java/com/exadel/aem/toolkit/plugin/utils/ToolchainUtil.java diff --git a/plugin/pom.xml b/plugin/pom.xml index 9b9b6a2bc..fe1cb4737 100644 --- a/plugin/pom.xml +++ b/plugin/pom.xml @@ -150,6 +150,10 @@ org.apache.maven maven-plugin-api + + org.codehaus.plexus + plexus-utils + org.mozilla rhino diff --git a/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginMojo.java b/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginMojo.java index bdd146923..3f0d47113 100644 --- a/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginMojo.java +++ b/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginMojo.java @@ -13,8 +13,12 @@ */ package com.exadel.aem.toolkit.plugin.maven; +import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.Optional; +import java.util.Set; +import java.util.regex.Pattern; import java.util.stream.Collectors; import org.apache.commons.lang3.StringUtils; @@ -24,54 +28,87 @@ import org.apache.maven.model.ConfigurationContainer; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugins.annotations.Component; 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.plugins.annotations.ResolutionScope; import org.apache.maven.project.MavenProject; +import org.apache.maven.toolchain.ToolchainManager; +import org.codehaus.plexus.util.cli.CommandLineException; +import org.codehaus.plexus.util.cli.CommandLineUtils; +import org.codehaus.plexus.util.cli.Commandline; import org.codehaus.plexus.util.xml.Xpp3Dom; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.exadel.aem.toolkit.core.CoreConstants; import com.exadel.aem.toolkit.plugin.exceptions.PluginException; import com.exadel.aem.toolkit.plugin.sources.ComponentSource; import com.exadel.aem.toolkit.plugin.utils.DialogConstants; +import com.exadel.aem.toolkit.plugin.utils.ToolchainUtil; import com.exadel.aem.toolkit.plugin.writers.PackageWriter; /** - * Represents the entry-point of the EToolbox Authoring Kit (the ToolKit) Maven plugin execution + * Represents the entry point of the EToolbox Authoring Kit (the ToolKit) Maven plugin execution */ -@Mojo(name = "aem-authoring", defaultPhase = LifecyclePhase.PACKAGE, requiresDependencyCollection = ResolutionScope.COMPILE) +@Mojo( + name = PluginMojo.PLUGIN_GOAL, + defaultPhase = LifecyclePhase.PACKAGE, + requiresDependencyCollection = ResolutionScope.COMPILE +) @SuppressWarnings({"unused", "MismatchedQueryAndUpdateOfCollection"}) public class PluginMojo extends AbstractMojo { private static final Logger LOG = LoggerFactory.getLogger(DialogConstants.ARTIFACT_NAME); private static final String PLUGIN_ARTIFACT_ID = "etoolbox-authoring-kit-plugin"; + static final String PLUGIN_GOAL = "aem-authoring"; + private static final String PLUGIN_GROUP = "com.exadel.etoolbox"; + + private static final String MAVEN_EXECUTABLE = "mvn"; + private static final String PROJECT_TYPE_PACKAGE = "content-package"; + + private static final String CONFIG_KEY_CLASSPATH_ELEMENTS = "classpathElements"; private static final String CONFIG_KEY_PATH_BASE = "componentsPathBase"; private static final String CONFIG_KEY_REFERENCE_BASE = "componentsReferenceBase"; + private static final String CONFIG_KEY_TERMINATE_ON = "terminateOn"; private static final String DEPENDENCY_RESOLUTION_EXCEPTION_MESSAGE = "Could not resolve dependencies of project %s: %s"; private static final String PLUGIN_EXECUTION_EXCEPTION_MESSAGE = "%s in module %s: %s"; private static final String PLUGIN_COMPLETION_MESSAGE = "Execution completed."; private static final String PLUGIN_COMPLETION_STATISTICS_MESSAGE = PLUGIN_COMPLETION_MESSAGE + " {} component(-s) processed."; - @Parameter(readonly = true, defaultValue = "${project}") + private static final String ARGUMENT_FORMAT = "-D%s=%s"; + private static final Pattern ARGUMENT_SPLITTER = Pattern.compile(CoreConstants.SEPARATOR_COMMA); + private static final String PATTERN_LOG_LEVEL = "^\\s*\\[[A-Z]+]\\s+"; + + @Component + private ToolchainManager toolchainManager; + + // Automatic parameters + + @Parameter(defaultValue = "${project}", readonly = true, required = true) private MavenProject project; - @Parameter(defaultValue = "${session}", required = true, readonly = true) + @Parameter(defaultValue = "${session}", readonly = true, required = true) private MavenSession session; - @Parameter(readonly = true, defaultValue = "${plugin.artifacts}") + @Parameter(defaultValue = "${plugin.artifacts}", readonly = true) private List pluginDependencies; - @Parameter(readonly = true) + // Customizable parameters + + @Parameter(property = "classpathElements", readonly = true) + private String classpathElements; + + @Parameter(property = "componentsPathBase", readonly = true) private String componentsPathBase; - @Parameter(readonly = true) + @Parameter(property = "componentsReferenceBase", readonly = true) private String componentsReferenceBase; - @Parameter(readonly = true, defaultValue = "java.io.IOException") + @Parameter(defaultValue = "java.io.IOException", property = "terminateOn", readonly = true) private String terminateOn; /** @@ -80,29 +117,27 @@ public class PluginMojo extends AbstractMojo { * extracted and processed with {@link PackageWriter} instance created for a particular Maven project; the result is * written down to the AEM package zip file. The method is run once for each package module that has the ToolKit * plugin included in the POM file - * @throws MojoExecutionException if work on a package cannot proceed (due to e.g. file system failure or improper + * @throws MojoExecutionException if work on a package cannot proceed (due to, e.g., file system failure or improper * initialization) or in case an internal exception is thrown that corresponds to the * {@code terminateOn} setting */ public void execute() throws MojoExecutionException { - List classpathElements; - try { - classpathElements = project.getCompileClasspathElements(); - } catch (DependencyResolutionRequiredException e) { - throw new MojoExecutionException(String.format(DEPENDENCY_RESOLUTION_EXCEPTION_MESSAGE, - project.getBuild().getFinalName(), - e.getMessage()), e); + if (ToolchainUtil.shouldReload(toolchainManager, session)) { + String javaHome = ToolchainUtil.getJavaHome(toolchainManager, session); + LOG.info("Another JVM version is required. Will use {}", javaHome); + fork(javaHome); + return; } - pluginDependencies.stream().findFirst().ifPresent(d -> classpathElements.add(d.getFile().getPath())); PluginSettings.Builder settingsBuilder = PluginSettings.builder() .terminateOn(terminateOn) .defaultPathBase(componentsPathBase); populateReferenceEntries(settingsBuilder); + PluginSettings pluginSettings = settingsBuilder.build(); PluginRuntime.contextBuilder() - .classPathElements(classpathElements) - .settings(settingsBuilder.build()) + .classPathElements(getClasspathElements()) + .settings(pluginSettings) .build(); int processedCount = 0; @@ -127,6 +162,59 @@ public void execute() throws MojoExecutionException { } } + /** + * Restarts the ToolKit's plugin execution with the JDK specified in the current Maven toolchain + * @param jvmPath String value representing the path to the JDK + * @throws MojoExecutionException if the plugin process cannot be restarted due to, e.g., missing dependencies + */ + private void fork(String jvmPath) throws MojoExecutionException { + Commandline commandline = new Commandline(); + commandline.setExecutable(MAVEN_EXECUTABLE); + commandline.addEnvironment(DialogConstants.PN_JAVA_HOME, jvmPath); + commandline.addArguments(new String[] { + String.join(CoreConstants.SEPARATOR_COLON, PLUGIN_GROUP, PLUGIN_ARTIFACT_ID, PLUGIN_GOAL), + String.format(ARGUMENT_FORMAT, CONFIG_KEY_CLASSPATH_ELEMENTS, String.join(CoreConstants.SEPARATOR_COMMA, getClasspathElements())), + String.format(ARGUMENT_FORMAT, CONFIG_KEY_PATH_BASE, componentsPathBase), + String.format(ARGUMENT_FORMAT, CONFIG_KEY_REFERENCE_BASE, componentsReferenceBase), + String.format(ARGUMENT_FORMAT, CONFIG_KEY_TERMINATE_ON, terminateOn) + }); + commandline.setWorkingDirectory(project.getFile().getParentFile()); + LOG.info("Restarting with {}", commandline); + try { + CommandLineUtils.executeCommandLine( + commandline, + line -> LOG.info(line.replaceAll(PATTERN_LOG_LEVEL, StringUtils.EMPTY)), + line -> LOG.error(line.replaceAll(PATTERN_LOG_LEVEL, StringUtils.EMPTY))); + } catch (CommandLineException e) { + throw new MojoExecutionException("Could not restart plugin process", e); + } + } + + /** + * Retrieves the list of classpath elements for the current Maven project + * @return {@code List} of {@code String} values + * @throws MojoExecutionException if required dependencies cannot be resolved + */ + private List getClasspathElements() throws MojoExecutionException { + Set result; + try { + result = new HashSet<>(project.getCompileClasspathElements()); + } catch (DependencyResolutionRequiredException e) { + throw new MojoExecutionException(String.format(DEPENDENCY_RESOLUTION_EXCEPTION_MESSAGE, + project.getBuild().getFinalName(), + e.getMessage()), e); + } + if (StringUtils.isNotBlank(this.classpathElements)) { + ARGUMENT_SPLITTER + .splitAsStream(this.classpathElements) + .filter(StringUtils::isNotBlank) + .map(String::trim) + .forEach(result::add); + } + pluginDependencies.stream().findFirst().ifPresent(d -> result.add(d.getFile().getPath())); + return new ArrayList<>(result); + } + /** * Scans the module structure of the current Maven installation to retrieve the ToolKit's plugin configurations and * stores the matches between AEM component Java packages and repository paths. The references are passed to the diff --git a/plugin/src/main/java/com/exadel/aem/toolkit/plugin/utils/DialogConstants.java b/plugin/src/main/java/com/exadel/aem/toolkit/plugin/utils/DialogConstants.java index d4a85fd41..37e550672 100644 --- a/plugin/src/main/java/com/exadel/aem/toolkit/plugin/utils/DialogConstants.java +++ b/plugin/src/main/java/com/exadel/aem/toolkit/plugin/utils/DialogConstants.java @@ -121,6 +121,7 @@ public class DialogConstants { public static final String PN_GROUPS = "groups"; public static final String PN_IGNORE_MODE = "ignoreMode"; public static final String PN_INDENT_SIZE = "indentSize"; + public static final String PN_JAVA_HOME = "java.home"; public static final String PN_JCR_TITLE = "jcr:title"; public static final String PN_MAX_DATE = "maxDate"; public static final String PN_MAX_UNDO_STEPS = "maxUndoSteps"; diff --git a/plugin/src/main/java/com/exadel/aem/toolkit/plugin/utils/ToolchainUtil.java b/plugin/src/main/java/com/exadel/aem/toolkit/plugin/utils/ToolchainUtil.java new file mode 100644 index 000000000..b6427b94d --- /dev/null +++ b/plugin/src/main/java/com/exadel/aem/toolkit/plugin/utils/ToolchainUtil.java @@ -0,0 +1,72 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.exadel.aem.toolkit.plugin.utils; + +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.apache.commons.lang3.StringUtils; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.toolchain.Toolchain; +import org.apache.maven.toolchain.ToolchainManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Contains utility methods for working with Maven toolchain + */ +public class ToolchainUtil { + private static final Logger LOG = LoggerFactory.getLogger(ToolchainUtil.class); + + private static final String DIRECTORY_BIN = "bin"; + private static final String TOOL_JAVA = "java"; + private static final String TYPE_JDK = "jdk"; + + /** + * Default (instantiation-restricting) constructor + */ + private ToolchainUtil() { + } + + /** + * Gets whether the ToolKit's plugin should reload with the JDK specified in the current Maven toolchain + * @param toolchainManager {@link ToolchainManager} instance + * @param session {@link MavenSession} instance + * @return True or false + */ + public static boolean shouldReload(ToolchainManager toolchainManager, MavenSession session) { + String javaHome = System.getProperty(DialogConstants.PN_JAVA_HOME); + String toolchainJavaHome = getJavaHome(toolchainManager, session); + return StringUtils.isNoneEmpty(javaHome, toolchainJavaHome) && !StringUtils.equals(javaHome, toolchainJavaHome); + } + + /** + * Gets the path to the JDK specified in the current Maven toolchain + * @param toolchainManager {@link ToolchainManager} instance + * @param session {@link MavenSession} instance + * @return {@code String} value, or empty string if not found + */ + public static String getJavaHome(ToolchainManager toolchainManager, MavenSession session) { + Toolchain jdkToolchain = toolchainManager.getToolchainFromBuildContext(TYPE_JDK, session); + String javaPath = jdkToolchain != null ? jdkToolchain.findTool(TOOL_JAVA) : null; + if (javaPath != null) { + Path path = Paths.get(javaPath).getParent(); + if (DIRECTORY_BIN.equals(path.getFileName().toString())) { + path = path.getParent(); + } + return path.toAbsolutePath().toString(); + } + return StringUtils.EMPTY; + } +} diff --git a/pom.xml b/pom.xml index 132303947..d2c5ae8a2 100644 --- a/pom.xml +++ b/pom.xml @@ -606,6 +606,12 @@ 2.1.10-1.16.0 test + + org.codehaus.plexus + plexus-utils + 3.5.1 + compile + org.mockito mockito-core From 53c3f32300e0843eb807cacf4fc3deed93a0901b Mon Sep 17 00:00:00 2001 From: Stepan Miakchilo Date: Wed, 22 Nov 2023 13:33:45 +0300 Subject: [PATCH 2/7] [EAK-497] Minor linter warning fix --- .../java/com/exadel/aem/toolkit/plugin/maven/PluginMojo.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginMojo.java b/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginMojo.java index 3f0d47113..cfa13b77f 100644 --- a/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginMojo.java +++ b/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginMojo.java @@ -61,8 +61,8 @@ public class PluginMojo extends AbstractMojo { private static final Logger LOG = LoggerFactory.getLogger(DialogConstants.ARTIFACT_NAME); - private static final String PLUGIN_ARTIFACT_ID = "etoolbox-authoring-kit-plugin"; static final String PLUGIN_GOAL = "aem-authoring"; + private static final String PLUGIN_ARTIFACT_ID = "etoolbox-authoring-kit-plugin"; private static final String PLUGIN_GROUP = "com.exadel.etoolbox"; private static final String MAVEN_EXECUTABLE = "mvn"; From a317d8f8679c9ff727c99f575aa69d846df3595f Mon Sep 17 00:00:00 2001 From: Stepan Miakchilo Date: Wed, 22 Nov 2023 15:43:04 +0300 Subject: [PATCH 3/7] [EAK-497] Added cleaning output from a secondary Maven process --- .../aem/toolkit/plugin/maven/PluginMojo.java | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginMojo.java b/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginMojo.java index cfa13b77f..3cb23784e 100644 --- a/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginMojo.java +++ b/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginMojo.java @@ -18,9 +18,11 @@ import java.util.List; import java.util.Optional; import java.util.Set; +import java.util.function.Consumer; import java.util.regex.Pattern; import java.util.stream.Collectors; +import org.apache.commons.lang3.RegExUtils; import org.apache.commons.lang3.StringUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.DependencyResolutionRequiredException; @@ -80,8 +82,10 @@ public class PluginMojo extends AbstractMojo { private static final String PLUGIN_COMPLETION_STATISTICS_MESSAGE = PLUGIN_COMPLETION_MESSAGE + " {} component(-s) processed."; private static final String ARGUMENT_FORMAT = "-D%s=%s"; - private static final Pattern ARGUMENT_SPLITTER = Pattern.compile(CoreConstants.SEPARATOR_COMMA); + + private static final String PATTERN_COLOR_CODE = "[^A-Za-z0-0]\\[[0-9;]*m"; private static final String PATTERN_LOG_LEVEL = "^\\s*\\[[A-Z]+]\\s+"; + private static final Pattern PATTERN_SPLITTER = Pattern.compile(CoreConstants.SEPARATOR_COMMA); @Component private ToolchainManager toolchainManager; @@ -172,6 +176,7 @@ private void fork(String jvmPath) throws MojoExecutionException { commandline.setExecutable(MAVEN_EXECUTABLE); commandline.addEnvironment(DialogConstants.PN_JAVA_HOME, jvmPath); commandline.addArguments(new String[] { + "--batch-mode", String.join(CoreConstants.SEPARATOR_COLON, PLUGIN_GROUP, PLUGIN_ARTIFACT_ID, PLUGIN_GOAL), String.format(ARGUMENT_FORMAT, CONFIG_KEY_CLASSPATH_ELEMENTS, String.join(CoreConstants.SEPARATOR_COMMA, getClasspathElements())), String.format(ARGUMENT_FORMAT, CONFIG_KEY_PATH_BASE, componentsPathBase), @@ -183,8 +188,8 @@ private void fork(String jvmPath) throws MojoExecutionException { try { CommandLineUtils.executeCommandLine( commandline, - line -> LOG.info(line.replaceAll(PATTERN_LOG_LEVEL, StringUtils.EMPTY)), - line -> LOG.error(line.replaceAll(PATTERN_LOG_LEVEL, StringUtils.EMPTY))); + line -> relayLogLine(LOG::info, line), + line -> relayLogLine(LOG::error, line)); } catch (CommandLineException e) { throw new MojoExecutionException("Could not restart plugin process", e); } @@ -205,7 +210,7 @@ private List getClasspathElements() throws MojoExecutionException { e.getMessage()), e); } if (StringUtils.isNotBlank(this.classpathElements)) { - ARGUMENT_SPLITTER + PATTERN_SPLITTER .splitAsStream(this.classpathElements) .filter(StringUtils::isNotBlank) .map(String::trim) @@ -250,4 +255,19 @@ private void populateReferenceEntries(PluginSettings.Builder builder) { builder.referenceEntry(pathBase, referenceBase); } } + + /** + * Transfers to the main logger a line retrieved from the secondary Maven process + * @param logger A logging method such as {@code .info()} or {@code .error()} + * @param line A string value representing the line to log + */ + private static void relayLogLine(Consumer logger, String line) { + String effectiveLine = RegExUtils.removePattern(line, PATTERN_LOG_LEVEL); + effectiveLine = RegExUtils.removePattern(effectiveLine, PATTERN_COLOR_CODE); + if (StringUtils.isEmpty(effectiveLine)) { + return; + } + logger.accept(effectiveLine.trim()); + } } + From df7843078b6e3fefe3dce0742620fd5c53c43683 Mon Sep 17 00:00:00 2001 From: Stepan Miakchilo Date: Wed, 22 Nov 2023 23:43:52 +0300 Subject: [PATCH 4/7] [EAK-497] Reorganized JVM-related logic --- .../aem/toolkit/plugin/maven/PluginMojo.java | 19 +++++---- .../toolkit/plugin/utils/DialogConstants.java | 1 - .../{ToolchainUtil.java => JvmUtil.java} | 42 ++++++++++++------- 3 files changed, 36 insertions(+), 26 deletions(-) rename plugin/src/main/java/com/exadel/aem/toolkit/plugin/utils/{ToolchainUtil.java => JvmUtil.java} (67%) diff --git a/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginMojo.java b/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginMojo.java index 3cb23784e..4cd1dd8b8 100644 --- a/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginMojo.java +++ b/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginMojo.java @@ -48,7 +48,7 @@ import com.exadel.aem.toolkit.plugin.exceptions.PluginException; import com.exadel.aem.toolkit.plugin.sources.ComponentSource; import com.exadel.aem.toolkit.plugin.utils.DialogConstants; -import com.exadel.aem.toolkit.plugin.utils.ToolchainUtil; +import com.exadel.aem.toolkit.plugin.utils.JvmUtil; import com.exadel.aem.toolkit.plugin.writers.PackageWriter; /** @@ -126,10 +126,11 @@ public class PluginMojo extends AbstractMojo { * {@code terminateOn} setting */ public void execute() throws MojoExecutionException { - if (ToolchainUtil.shouldReload(toolchainManager, session)) { - String javaHome = ToolchainUtil.getJavaHome(toolchainManager, session); - LOG.info("Another JVM version is required. Will use {}", javaHome); - fork(javaHome); + if (JvmUtil.shouldRelaunch(toolchainManager, session)) { + String currentJavaHome = JvmUtil.getJavaHome(); + String toolchainJavaHome = JvmUtil.getJavaHome(toolchainManager, session); + LOG.info("Current JVM is {}. Will switch to {}", currentJavaHome, toolchainJavaHome); + fork(toolchainJavaHome); return; } @@ -174,7 +175,8 @@ public void execute() throws MojoExecutionException { private void fork(String jvmPath) throws MojoExecutionException { Commandline commandline = new Commandline(); commandline.setExecutable(MAVEN_EXECUTABLE); - commandline.addEnvironment(DialogConstants.PN_JAVA_HOME, jvmPath); + commandline.addEnvironment(JvmUtil.PROPERTY_JAVA_HOME, jvmPath); + commandline.setWorkingDirectory(project.getFile().getParentFile()); commandline.addArguments(new String[] { "--batch-mode", String.join(CoreConstants.SEPARATOR_COLON, PLUGIN_GROUP, PLUGIN_ARTIFACT_ID, PLUGIN_GOAL), @@ -183,15 +185,14 @@ private void fork(String jvmPath) throws MojoExecutionException { String.format(ARGUMENT_FORMAT, CONFIG_KEY_REFERENCE_BASE, componentsReferenceBase), String.format(ARGUMENT_FORMAT, CONFIG_KEY_TERMINATE_ON, terminateOn) }); - commandline.setWorkingDirectory(project.getFile().getParentFile()); - LOG.info("Restarting with {}", commandline); + LOG.info("Relaunching plugin with {}", commandline); try { CommandLineUtils.executeCommandLine( commandline, line -> relayLogLine(LOG::info, line), line -> relayLogLine(LOG::error, line)); } catch (CommandLineException e) { - throw new MojoExecutionException("Could not restart plugin process", e); + throw new MojoExecutionException("Could not relaunch plugin process", e); } } diff --git a/plugin/src/main/java/com/exadel/aem/toolkit/plugin/utils/DialogConstants.java b/plugin/src/main/java/com/exadel/aem/toolkit/plugin/utils/DialogConstants.java index 37e550672..d4a85fd41 100644 --- a/plugin/src/main/java/com/exadel/aem/toolkit/plugin/utils/DialogConstants.java +++ b/plugin/src/main/java/com/exadel/aem/toolkit/plugin/utils/DialogConstants.java @@ -121,7 +121,6 @@ public class DialogConstants { public static final String PN_GROUPS = "groups"; public static final String PN_IGNORE_MODE = "ignoreMode"; public static final String PN_INDENT_SIZE = "indentSize"; - public static final String PN_JAVA_HOME = "java.home"; public static final String PN_JCR_TITLE = "jcr:title"; public static final String PN_MAX_DATE = "maxDate"; public static final String PN_MAX_UNDO_STEPS = "maxUndoSteps"; diff --git a/plugin/src/main/java/com/exadel/aem/toolkit/plugin/utils/ToolchainUtil.java b/plugin/src/main/java/com/exadel/aem/toolkit/plugin/utils/JvmUtil.java similarity index 67% rename from plugin/src/main/java/com/exadel/aem/toolkit/plugin/utils/ToolchainUtil.java rename to plugin/src/main/java/com/exadel/aem/toolkit/plugin/utils/JvmUtil.java index b6427b94d..b8fec880a 100644 --- a/plugin/src/main/java/com/exadel/aem/toolkit/plugin/utils/ToolchainUtil.java +++ b/plugin/src/main/java/com/exadel/aem/toolkit/plugin/utils/JvmUtil.java @@ -20,14 +20,13 @@ import org.apache.maven.execution.MavenSession; import org.apache.maven.toolchain.Toolchain; import org.apache.maven.toolchain.ToolchainManager; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** - * Contains utility methods for working with Maven toolchain + * Contains utility methods for working with JVM settings */ -public class ToolchainUtil { - private static final Logger LOG = LoggerFactory.getLogger(ToolchainUtil.class); +public class JvmUtil { + + public static final String PROPERTY_JAVA_HOME = "JAVA_HOME"; private static final String DIRECTORY_BIN = "bin"; private static final String TOOL_JAVA = "java"; @@ -36,25 +35,23 @@ public class ToolchainUtil { /** * Default (instantiation-restricting) constructor */ - private ToolchainUtil() { + private JvmUtil() { } /** - * Gets whether the ToolKit's plugin should reload with the JDK specified in the current Maven toolchain - * @param toolchainManager {@link ToolchainManager} instance - * @param session {@link MavenSession} instance - * @return True or false + * Gets the path to the Java installation specified in the current environment + * @return {@code String} value, or empty string if not found */ - public static boolean shouldReload(ToolchainManager toolchainManager, MavenSession session) { - String javaHome = System.getProperty(DialogConstants.PN_JAVA_HOME); - String toolchainJavaHome = getJavaHome(toolchainManager, session); - return StringUtils.isNoneEmpty(javaHome, toolchainJavaHome) && !StringUtils.equals(javaHome, toolchainJavaHome); + public static String getJavaHome() { + return StringUtils.firstNonBlank( + System.getProperty(PROPERTY_JAVA_HOME), + System.getProperty("java.home")); } /** - * Gets the path to the JDK specified in the current Maven toolchain + * Gets the path to the Java installation specified in the current Maven toolchain * @param toolchainManager {@link ToolchainManager} instance - * @param session {@link MavenSession} instance + * @param session {@link MavenSession} instance * @return {@code String} value, or empty string if not found */ public static String getJavaHome(ToolchainManager toolchainManager, MavenSession session) { @@ -69,4 +66,17 @@ public static String getJavaHome(ToolchainManager toolchainManager, MavenSession } return StringUtils.EMPTY; } + + /** + * Gets whether the ToolKit's plugin needs to be launched in a separate process with the JDK specified in the + * current Maven toolchain + * @param toolchainManager {@link ToolchainManager} instance + * @param session {@link MavenSession} instance + * @return True or false + */ + public static boolean shouldRelaunch(ToolchainManager toolchainManager, MavenSession session) { + String javaHome = getJavaHome(); + String toolchainJavaHome = getJavaHome(toolchainManager, session); + return StringUtils.isNoneEmpty(javaHome, toolchainJavaHome) && !StringUtils.equals(javaHome, toolchainJavaHome); + } } From d346cbf6a8f0aaad89ec7e2ad4d60867feacaf49 Mon Sep 17 00:00:00 2001 From: Stepan Miakchilo Date: Fri, 24 Nov 2023 13:12:45 +0300 Subject: [PATCH 5/7] [EAK-497] Switched to using classworlds-based Maven plugin launch --- .../aem/toolkit/plugin/maven/JvmHelper.java | 218 ++++++++++++++++++ .../aem/toolkit/plugin/maven/PluginMojo.java | 48 ++-- .../aem/toolkit/plugin/utils/JvmUtil.java | 82 ------- 3 files changed, 241 insertions(+), 107 deletions(-) create mode 100644 plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/JvmHelper.java delete mode 100644 plugin/src/main/java/com/exadel/aem/toolkit/plugin/utils/JvmUtil.java diff --git a/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/JvmHelper.java b/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/JvmHelper.java new file mode 100644 index 000000000..dfc2ad1ce --- /dev/null +++ b/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/JvmHelper.java @@ -0,0 +1,218 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.exadel.aem.toolkit.plugin.maven; + +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.commons.lang3.StringUtils; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.toolchain.Toolchain; +import org.apache.maven.toolchain.ToolchainManager; +import org.codehaus.plexus.util.cli.Commandline; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Contains utility methods for working with JVM settings + */ +class JvmHelper { + + private static final Logger LOG = LoggerFactory.getLogger(JvmHelper.class); + + private static final String PROPERTY_MAVEN_HOME = "maven.home"; + private static final String PROPERTY_CLASSWORLDS = "classworlds.conf"; + private static final String PROPERTY_CLASS_PATH = "java.class.path"; + + private static final String ARGUMENT_FORMAT = "-D%s=%s"; + + /** + * Default (instantiation-restricting) constructor + */ + private JvmHelper() { + } + + /** + * Gets whether the ToolKit's plugin needs to be launched in a separate process with the JDK specified in the + * current Maven toolchain + * @param toolchainManager {@link ToolchainManager} instance + * @param session {@link MavenSession} instance + * @return True or false + */ + static boolean shouldRelaunch(ToolchainManager toolchainManager, MavenSession session) { + String javaHome = getJavaHome(); + String toolchainJavaHome = getJavaHome(toolchainManager, session); + LOG.info("Java home: {}", javaHome); + LOG.info("Toolchain Java home: {}", toolchainJavaHome); + if (StringUtils.isAnyEmpty(javaHome, toolchainJavaHome) || StringUtils.equals(javaHome, toolchainJavaHome)) { + return false; + } + return validateProperties(); + } + + /** + * Retrieves the name of a Java executable per the current toolchain + * @param toolchainManager {@link ToolchainManager} instance + * @param session {@link MavenSession} instance + * @return String value; can be empty if a toolchain is not set up + */ + static String getJavaExecutable(ToolchainManager toolchainManager, MavenSession session) { + Toolchain jdkToolchain = toolchainManager.getToolchainFromBuildContext("jdk", session); + return jdkToolchain != null ? jdkToolchain.findTool("java") : StringUtils.EMPTY; + } + + /** + * Gets the path to the Java installation specified in the current environment + * @return {@code String} value, or empty string if not found + */ + static String getJavaHome() { + return StringUtils.firstNonBlank( + System.getProperty("JAVA_HOME"), + System.getProperty("java.home")); + } + + /** + * Gets the path to the Java installation specified in the current Maven toolchain + * @param executable Path to the {@code java} binary + * @return String value, or an empty string if the provided executable is invalid + */ + static String getJavaHome(String executable) { + Path result = Paths.get(executable).getParent(); + if ("bin".equals(result.getFileName().toString())) { + result = result.getParent(); + } + return result.toAbsolutePath().toString(); + } + + /** + * Gets the path to the Java installation specified in the current Maven toolchain + * @param toolchainManager {@link ToolchainManager} instance + * @param session {@link MavenSession} instance + * @return String value, or an empty string if not found + */ + private static String getJavaHome(ToolchainManager toolchainManager, MavenSession session) { + String executable = getJavaExecutable(toolchainManager, session); + if (StringUtils.isEmpty(executable)) { + return StringUtils.EMPTY; + } + return getJavaHome(executable); + } + + /** + * Validates that the system properties required for successful JVM process forking are set + * @return True or false + */ + private static boolean validateProperties() { + boolean result = true; + for (String property : Arrays.asList(PROPERTY_CLASS_PATH, PROPERTY_CLASSWORLDS, PROPERTY_MAVEN_HOME)) { + if (StringUtils.isEmpty(System.getProperty(property))) { + LOG.warn("Property {} is not defined", property); + result = false; + } + } + return result; + } + + /** + * Composes a {@link Commandline} instance to launch the ToolKit's plugin in a separate JVM process + * @return {@link CommandLineBuilder} object + */ + static CommandLineBuilder commandLine() { + return new CommandLineBuilder(); + } + + /** + * Builds a {@link Commandline} instance to launch the ToolKit's plugin in a separate JVM process + */ + static class CommandLineBuilder { + + private String executable; + private String directory; + private String pluginCommand; + private Map arguments; + + /** + * Assigns the path to the {@code java} executable + * @param value String value; a non-empty string is expected + * @return This instance + */ + CommandLineBuilder executable(String value) { + executable = value; + return this; + } + + /** + * Assigns the working directory for the current Maven project + * @param value String value; a non-empty string is expected + * @return This instance + */ + CommandLineBuilder directory(String value) { + directory = value; + return this; + } + + /** + * Assigns the plugin command to be executed + * @param value String value; a non-empty string is expected + * @return This instance + */ + CommandLineBuilder pluginCommand(String value) { + pluginCommand = value; + return this; + } + + /** + * Assigns a particular argument to be passed to the plugin + * @param key The argument name + * @param value The argument value + * @return This instance + */ + CommandLineBuilder argument(String key, String value) { + if (arguments == null) { + arguments = new HashMap<>(); + } + arguments.put(key, value); + return this; + } + + /** + * Creates a {@link Commandline} instance based on the provided parameters + * @return {@code Commandline} object + */ + Commandline build() { + Commandline commandline = new Commandline(); + commandline.setExecutable(executable); + commandline.setWorkingDirectory(directory); + List commandLineArgs = new ArrayList<>(); + commandLineArgs.add(String.format(ARGUMENT_FORMAT, "maven.multiModuleProjectDirectory", directory)); + commandLineArgs.add(String.format(ARGUMENT_FORMAT, PROPERTY_MAVEN_HOME, System.getProperty(PROPERTY_MAVEN_HOME))); + commandLineArgs.add(String.format(ARGUMENT_FORMAT, PROPERTY_CLASSWORLDS, System.getProperty(PROPERTY_CLASSWORLDS))); + commandLineArgs.add("-classpath"); + commandLineArgs.add(System.getProperty(PROPERTY_CLASS_PATH)); + commandLineArgs.add("org.codehaus.classworlds.Launcher"); + commandLineArgs.add(pluginCommand); + if (arguments != null) { + arguments.forEach((key, value) -> commandLineArgs.add(String.format(ARGUMENT_FORMAT, key, value))); + } + commandLineArgs.add("--batch-mode"); + commandline.addArguments(commandLineArgs.toArray(new String[0])); + return commandline; + } + } +} diff --git a/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginMojo.java b/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginMojo.java index 4cd1dd8b8..ff41429e6 100644 --- a/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginMojo.java +++ b/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginMojo.java @@ -16,7 +16,9 @@ import java.util.ArrayList; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Optional; +import java.util.Properties; import java.util.Set; import java.util.function.Consumer; import java.util.regex.Pattern; @@ -48,7 +50,6 @@ import com.exadel.aem.toolkit.plugin.exceptions.PluginException; import com.exadel.aem.toolkit.plugin.sources.ComponentSource; import com.exadel.aem.toolkit.plugin.utils.DialogConstants; -import com.exadel.aem.toolkit.plugin.utils.JvmUtil; import com.exadel.aem.toolkit.plugin.writers.PackageWriter; /** @@ -67,8 +68,6 @@ public class PluginMojo extends AbstractMojo { private static final String PLUGIN_ARTIFACT_ID = "etoolbox-authoring-kit-plugin"; private static final String PLUGIN_GROUP = "com.exadel.etoolbox"; - private static final String MAVEN_EXECUTABLE = "mvn"; - private static final String PROJECT_TYPE_PACKAGE = "content-package"; private static final String CONFIG_KEY_CLASSPATH_ELEMENTS = "classpathElements"; @@ -81,8 +80,6 @@ public class PluginMojo extends AbstractMojo { private static final String PLUGIN_COMPLETION_MESSAGE = "Execution completed."; private static final String PLUGIN_COMPLETION_STATISTICS_MESSAGE = PLUGIN_COMPLETION_MESSAGE + " {} component(-s) processed."; - private static final String ARGUMENT_FORMAT = "-D%s=%s"; - private static final String PATTERN_COLOR_CODE = "[^A-Za-z0-0]\\[[0-9;]*m"; private static final String PATTERN_LOG_LEVEL = "^\\s*\\[[A-Z]+]\\s+"; private static final Pattern PATTERN_SPLITTER = Pattern.compile(CoreConstants.SEPARATOR_COMMA); @@ -126,11 +123,13 @@ public class PluginMojo extends AbstractMojo { * {@code terminateOn} setting */ public void execute() throws MojoExecutionException { - if (JvmUtil.shouldRelaunch(toolchainManager, session)) { - String currentJavaHome = JvmUtil.getJavaHome(); - String toolchainJavaHome = JvmUtil.getJavaHome(toolchainManager, session); - LOG.info("Current JVM is {}. Will switch to {}", currentJavaHome, toolchainJavaHome); - fork(toolchainJavaHome); + if (JvmHelper.shouldRelaunch(toolchainManager, session)) { + String toolchainJavaExec = JvmHelper.getJavaExecutable(toolchainManager, session); + LOG.info( + "Current JVM is {}. Will switch to {}", + JvmHelper.getJavaHome(), + JvmHelper.getJavaHome(toolchainJavaExec)); + fork(toolchainJavaExec); return; } @@ -169,22 +168,21 @@ public void execute() throws MojoExecutionException { /** * Restarts the ToolKit's plugin execution with the JDK specified in the current Maven toolchain - * @param jvmPath String value representing the path to the JDK - * @throws MojoExecutionException if the plugin process cannot be restarted due to, e.g., missing dependencies + * @param executable Path to Java executable; a non-blank string is expected + * @throws MojoExecutionException if the plugin process cannot be restarted due to, e.g., missing properties or + * dependencies */ - private void fork(String jvmPath) throws MojoExecutionException { - Commandline commandline = new Commandline(); - commandline.setExecutable(MAVEN_EXECUTABLE); - commandline.addEnvironment(JvmUtil.PROPERTY_JAVA_HOME, jvmPath); - commandline.setWorkingDirectory(project.getFile().getParentFile()); - commandline.addArguments(new String[] { - "--batch-mode", - String.join(CoreConstants.SEPARATOR_COLON, PLUGIN_GROUP, PLUGIN_ARTIFACT_ID, PLUGIN_GOAL), - String.format(ARGUMENT_FORMAT, CONFIG_KEY_CLASSPATH_ELEMENTS, String.join(CoreConstants.SEPARATOR_COMMA, getClasspathElements())), - String.format(ARGUMENT_FORMAT, CONFIG_KEY_PATH_BASE, componentsPathBase), - String.format(ARGUMENT_FORMAT, CONFIG_KEY_REFERENCE_BASE, componentsReferenceBase), - String.format(ARGUMENT_FORMAT, CONFIG_KEY_TERMINATE_ON, terminateOn) - }); + private void fork(String executable) throws MojoExecutionException { + Commandline commandline = JvmHelper + .commandLine() + .executable(executable) + .directory(project.getFile().getParent()) + .pluginCommand(String.join(CoreConstants.SEPARATOR_COLON, PLUGIN_GROUP, PLUGIN_ARTIFACT_ID, PLUGIN_GOAL)) + .argument(CONFIG_KEY_CLASSPATH_ELEMENTS, String.join(CoreConstants.SEPARATOR_COMMA, getClasspathElements())) + .argument(CONFIG_KEY_PATH_BASE, componentsPathBase) + .argument(CONFIG_KEY_REFERENCE_BASE, componentsReferenceBase) + .argument(CONFIG_KEY_TERMINATE_ON, terminateOn) + .build(); LOG.info("Relaunching plugin with {}", commandline); try { CommandLineUtils.executeCommandLine( diff --git a/plugin/src/main/java/com/exadel/aem/toolkit/plugin/utils/JvmUtil.java b/plugin/src/main/java/com/exadel/aem/toolkit/plugin/utils/JvmUtil.java deleted file mode 100644 index b8fec880a..000000000 --- a/plugin/src/main/java/com/exadel/aem/toolkit/plugin/utils/JvmUtil.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.exadel.aem.toolkit.plugin.utils; - -import java.nio.file.Path; -import java.nio.file.Paths; - -import org.apache.commons.lang3.StringUtils; -import org.apache.maven.execution.MavenSession; -import org.apache.maven.toolchain.Toolchain; -import org.apache.maven.toolchain.ToolchainManager; - -/** - * Contains utility methods for working with JVM settings - */ -public class JvmUtil { - - public static final String PROPERTY_JAVA_HOME = "JAVA_HOME"; - - private static final String DIRECTORY_BIN = "bin"; - private static final String TOOL_JAVA = "java"; - private static final String TYPE_JDK = "jdk"; - - /** - * Default (instantiation-restricting) constructor - */ - private JvmUtil() { - } - - /** - * Gets the path to the Java installation specified in the current environment - * @return {@code String} value, or empty string if not found - */ - public static String getJavaHome() { - return StringUtils.firstNonBlank( - System.getProperty(PROPERTY_JAVA_HOME), - System.getProperty("java.home")); - } - - /** - * Gets the path to the Java installation specified in the current Maven toolchain - * @param toolchainManager {@link ToolchainManager} instance - * @param session {@link MavenSession} instance - * @return {@code String} value, or empty string if not found - */ - public static String getJavaHome(ToolchainManager toolchainManager, MavenSession session) { - Toolchain jdkToolchain = toolchainManager.getToolchainFromBuildContext(TYPE_JDK, session); - String javaPath = jdkToolchain != null ? jdkToolchain.findTool(TOOL_JAVA) : null; - if (javaPath != null) { - Path path = Paths.get(javaPath).getParent(); - if (DIRECTORY_BIN.equals(path.getFileName().toString())) { - path = path.getParent(); - } - return path.toAbsolutePath().toString(); - } - return StringUtils.EMPTY; - } - - /** - * Gets whether the ToolKit's plugin needs to be launched in a separate process with the JDK specified in the - * current Maven toolchain - * @param toolchainManager {@link ToolchainManager} instance - * @param session {@link MavenSession} instance - * @return True or false - */ - public static boolean shouldRelaunch(ToolchainManager toolchainManager, MavenSession session) { - String javaHome = getJavaHome(); - String toolchainJavaHome = getJavaHome(toolchainManager, session); - return StringUtils.isNoneEmpty(javaHome, toolchainJavaHome) && !StringUtils.equals(javaHome, toolchainJavaHome); - } -} From b6ad93e273dde7e6581bde8848cd122dd5090146 Mon Sep 17 00:00:00 2001 From: Stepan Miakchilo Date: Fri, 24 Nov 2023 15:04:24 +0300 Subject: [PATCH 6/7] [EAK-497] Cosmetic Javadoc changes --- .../aem/toolkit/plugin/maven/PluginMojo.java | 2 -- .../runtime/ReflectionContextHelper.java | 23 ++++++++++--------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginMojo.java b/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginMojo.java index ff41429e6..136968dcf 100644 --- a/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginMojo.java +++ b/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginMojo.java @@ -16,9 +16,7 @@ import java.util.ArrayList; import java.util.HashSet; import java.util.List; -import java.util.Map; import java.util.Optional; -import java.util.Properties; import java.util.Set; import java.util.function.Consumer; import java.util.regex.Pattern; diff --git a/plugin/src/main/java/com/exadel/aem/toolkit/plugin/runtime/ReflectionContextHelper.java b/plugin/src/main/java/com/exadel/aem/toolkit/plugin/runtime/ReflectionContextHelper.java index f7eea3946..ee1064540 100644 --- a/plugin/src/main/java/com/exadel/aem/toolkit/plugin/runtime/ReflectionContextHelper.java +++ b/plugin/src/main/java/com/exadel/aem/toolkit/plugin/runtime/ReflectionContextHelper.java @@ -55,7 +55,7 @@ import com.exadel.aem.toolkit.plugin.utils.ordering.OrderingUtil; /** - * Introspects the classes available in Maven reactor to retrieve and manage Toolkit-related logic + * Introspects the classes available in the Maven reactor to retrieve and manage Toolkit-related logic */ public class ReflectionContextHelper { @@ -95,8 +95,9 @@ public ClassLoader getClassLoader() { /** * Retrieves a collection of unique {@code ComponentSource} objects that encapsulate {@code AemComponent}-annotated * and {@code @Dialog}-annotated classes - * @param packageBase Restricts the processing to certain package(-s) in the plugin's settings. Can help to e.g. - * separate between classes that are matched by component folders in the current content package + * @param packageBase Restricts the processing to the particular package(-s) in the plugin's settings. Can help to + * e.g. separate between classes that are matched by component folders in the current content + * package * @return A non-null list of {@code ComponentSource} objects; can be empty */ public List getComponents(String packageBase) { @@ -164,8 +165,8 @@ public ComponentSource getComponent(String path) { /** * Gets whether the given annotation has a managed handler or a meta-annotation. This method is useful for - * distinguishing between ToolKit-relevant annotations (including custom ones that reside in user's own namespace) - * and "foreign" annotations + * distinguishing between ToolKit-relevant annotations (including custom ones that reside in the user's own + * namespace) and "foreign" annotations * @param annotation {@link Annotation} object * @return True or false */ @@ -223,8 +224,8 @@ public List getHandlers() { } /** - * Tests whether the given handler fits for the conditions defined by the set of manageable annotations and the - * {@code Scope} value + * Tests whether the given handler is suitable for the conditions defined by the set of manageable annotations and + * the {@code Scope} value * @param scope String value representing the scope that the handlers must match * @param handler {@code Handler} instance to test * @param annotations An array of {@code Annotation} objects, usually representing annotations of a method or class @@ -235,8 +236,8 @@ private static boolean isHandlerMatches(Handler handler, String scope, Annotatio } /** - * Tests whether the given handler fits for the conditions defined by the set of manageable annotations and the - * {@code Scope} value + * Tests whether the given handler is suitable for the conditions defined by the set of manageable annotations and + * the {@code Scope} value * @param handler {@code Handler} instance to test * @param scope String value representing the scope that the handlers must match * @param annotationTypes An array of {@code Class} references, usually representing types of annotations of a @@ -259,12 +260,12 @@ private static boolean isHandlerMatches(Handler handler, String scope, Class[ String[] handlerScopes = handles != null ? handles.scope() : new String[]{Scopes.DEFAULT}; // Try to guess appropriate scopes for the handler judging by the annotations it handles - // (so that if it handles e.g. @ChildEditConfig, the scope for the handler is exactly ChildEditConfig) + // (so that if it handles, e.g., {@code @ChildEditConfig}, the scope for the handler is exactly ChildEditConfig) if (handles != null && handlerScopes.length == 1 && handlerScopes[0].equals(Scopes.DEFAULT)) { handlerScopes = ScopeUtil.designate(handles.value()); } // If still no particular scopes, try to guess by the mere annotations added to the current class - // (so that if there's e.g. @Dialog, and the handler has no particular scope, it is considered the handler + // (so that if there is, e.g., {@code @Dialog}, and the handler has no particular scope, it is considered the handler // is also for the dialog) if (handlerScopes.length == 1 && handlerScopes[0].equals(Scopes.DEFAULT)) { handlerScopes = ScopeUtil.designate(annotationTypes); From 7d696f889da5c28e0b411adb35f1ea9edf44f6e8 Mon Sep 17 00:00:00 2001 From: Stepan Miakchilo Date: Fri, 24 Nov 2023 15:04:24 +0300 Subject: [PATCH 7/7] [EAK-497] Cosmetic Javadoc changes; retired debug messages --- .../aem/toolkit/plugin/maven/JvmHelper.java | 2 -- .../aem/toolkit/plugin/maven/PluginMojo.java | 2 -- .../runtime/ReflectionContextHelper.java | 23 ++++++++++--------- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/JvmHelper.java b/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/JvmHelper.java index dfc2ad1ce..049c5e3f9 100644 --- a/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/JvmHelper.java +++ b/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/JvmHelper.java @@ -58,8 +58,6 @@ private JvmHelper() { static boolean shouldRelaunch(ToolchainManager toolchainManager, MavenSession session) { String javaHome = getJavaHome(); String toolchainJavaHome = getJavaHome(toolchainManager, session); - LOG.info("Java home: {}", javaHome); - LOG.info("Toolchain Java home: {}", toolchainJavaHome); if (StringUtils.isAnyEmpty(javaHome, toolchainJavaHome) || StringUtils.equals(javaHome, toolchainJavaHome)) { return false; } diff --git a/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginMojo.java b/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginMojo.java index ff41429e6..136968dcf 100644 --- a/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginMojo.java +++ b/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginMojo.java @@ -16,9 +16,7 @@ import java.util.ArrayList; import java.util.HashSet; import java.util.List; -import java.util.Map; import java.util.Optional; -import java.util.Properties; import java.util.Set; import java.util.function.Consumer; import java.util.regex.Pattern; diff --git a/plugin/src/main/java/com/exadel/aem/toolkit/plugin/runtime/ReflectionContextHelper.java b/plugin/src/main/java/com/exadel/aem/toolkit/plugin/runtime/ReflectionContextHelper.java index f7eea3946..ee1064540 100644 --- a/plugin/src/main/java/com/exadel/aem/toolkit/plugin/runtime/ReflectionContextHelper.java +++ b/plugin/src/main/java/com/exadel/aem/toolkit/plugin/runtime/ReflectionContextHelper.java @@ -55,7 +55,7 @@ import com.exadel.aem.toolkit.plugin.utils.ordering.OrderingUtil; /** - * Introspects the classes available in Maven reactor to retrieve and manage Toolkit-related logic + * Introspects the classes available in the Maven reactor to retrieve and manage Toolkit-related logic */ public class ReflectionContextHelper { @@ -95,8 +95,9 @@ public ClassLoader getClassLoader() { /** * Retrieves a collection of unique {@code ComponentSource} objects that encapsulate {@code AemComponent}-annotated * and {@code @Dialog}-annotated classes - * @param packageBase Restricts the processing to certain package(-s) in the plugin's settings. Can help to e.g. - * separate between classes that are matched by component folders in the current content package + * @param packageBase Restricts the processing to the particular package(-s) in the plugin's settings. Can help to + * e.g. separate between classes that are matched by component folders in the current content + * package * @return A non-null list of {@code ComponentSource} objects; can be empty */ public List getComponents(String packageBase) { @@ -164,8 +165,8 @@ public ComponentSource getComponent(String path) { /** * Gets whether the given annotation has a managed handler or a meta-annotation. This method is useful for - * distinguishing between ToolKit-relevant annotations (including custom ones that reside in user's own namespace) - * and "foreign" annotations + * distinguishing between ToolKit-relevant annotations (including custom ones that reside in the user's own + * namespace) and "foreign" annotations * @param annotation {@link Annotation} object * @return True or false */ @@ -223,8 +224,8 @@ public List getHandlers() { } /** - * Tests whether the given handler fits for the conditions defined by the set of manageable annotations and the - * {@code Scope} value + * Tests whether the given handler is suitable for the conditions defined by the set of manageable annotations and + * the {@code Scope} value * @param scope String value representing the scope that the handlers must match * @param handler {@code Handler} instance to test * @param annotations An array of {@code Annotation} objects, usually representing annotations of a method or class @@ -235,8 +236,8 @@ private static boolean isHandlerMatches(Handler handler, String scope, Annotatio } /** - * Tests whether the given handler fits for the conditions defined by the set of manageable annotations and the - * {@code Scope} value + * Tests whether the given handler is suitable for the conditions defined by the set of manageable annotations and + * the {@code Scope} value * @param handler {@code Handler} instance to test * @param scope String value representing the scope that the handlers must match * @param annotationTypes An array of {@code Class} references, usually representing types of annotations of a @@ -259,12 +260,12 @@ private static boolean isHandlerMatches(Handler handler, String scope, Class[ String[] handlerScopes = handles != null ? handles.scope() : new String[]{Scopes.DEFAULT}; // Try to guess appropriate scopes for the handler judging by the annotations it handles - // (so that if it handles e.g. @ChildEditConfig, the scope for the handler is exactly ChildEditConfig) + // (so that if it handles, e.g., {@code @ChildEditConfig}, the scope for the handler is exactly ChildEditConfig) if (handles != null && handlerScopes.length == 1 && handlerScopes[0].equals(Scopes.DEFAULT)) { handlerScopes = ScopeUtil.designate(handles.value()); } // If still no particular scopes, try to guess by the mere annotations added to the current class - // (so that if there's e.g. @Dialog, and the handler has no particular scope, it is considered the handler + // (so that if there is, e.g., {@code @Dialog}, and the handler has no particular scope, it is considered the handler // is also for the dialog) if (handlerScopes.length == 1 && handlerScopes[0].equals(Scopes.DEFAULT)) { handlerScopes = ScopeUtil.designate(annotationTypes);