diff --git a/org.eclipse.m2e.jdt.tests/projects/surefireFailsafeToTestLaunchSettings/argumentsAreSet/failsafeArgsSet.xml b/org.eclipse.m2e.jdt.tests/projects/surefireFailsafeToTestLaunchSettings/argumentsAreSet/failsafeArgsSet.xml deleted file mode 100644 index 3c00b5cfd1..0000000000 --- a/org.eclipse.m2e.jdt.tests/projects/surefireFailsafeToTestLaunchSettings/argumentsAreSet/failsafeArgsSet.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - --argLineItem=failsafeArgLineValue - - - failsafeProp1Value - - - failsafeEnvironmentVariables1Value - - \ No newline at end of file diff --git a/org.eclipse.m2e.jdt.tests/projects/surefireFailsafeToTestLaunchSettings/argumentsAreSet/surefireArgsSet.xml b/org.eclipse.m2e.jdt.tests/projects/surefireFailsafeToTestLaunchSettings/argumentsAreSet/surefireArgsSet.xml deleted file mode 100644 index 303a1ae7b9..0000000000 --- a/org.eclipse.m2e.jdt.tests/projects/surefireFailsafeToTestLaunchSettings/argumentsAreSet/surefireArgsSet.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - --argLineItem=surefireArgLineValue - - - surefireProp1Value - - - surefireEnvironmentVariables1Value - - \ No newline at end of file diff --git a/org.eclipse.m2e.jdt.tests/projects/surefireFailsafeToTestLaunchSettings/prerequisitesAreLoaded/pom.xml b/org.eclipse.m2e.jdt.tests/projects/surefireFailsafeToTestLaunchSettings/prerequisitesAreLoaded/pom.xml deleted file mode 100644 index 6a094cd9a7..0000000000 --- a/org.eclipse.m2e.jdt.tests/projects/surefireFailsafeToTestLaunchSettings/prerequisitesAreLoaded/pom.xml +++ /dev/null @@ -1,51 +0,0 @@ - - - 4.0.0 - foo.bar - demo-test-config - 0.0.1-SNAPSHOT - - - - org.codehaus.mojo:properties-maven-plugin:read-project-properties - - - - - - org.codehaus.mojo - properties-maven-plugin - 1.2.1 - - - props_to_load.properties - - - - - read-project-properties - initialize - - read-project-properties - - - - - - - - - org.apache.maven.plugins - maven-surefire-plugin - - - --argLineItem=${property.to.load} - - - - - - - diff --git a/org.eclipse.m2e.jdt.tests/projects/surefireFailsafeToTestLaunchSettings/prerequisitesAreLoaded/props_to_load.properties b/org.eclipse.m2e.jdt.tests/projects/surefireFailsafeToTestLaunchSettings/prerequisitesAreLoaded/props_to_load.properties deleted file mode 100644 index f12a2d13a4..0000000000 --- a/org.eclipse.m2e.jdt.tests/projects/surefireFailsafeToTestLaunchSettings/prerequisitesAreLoaded/props_to_load.properties +++ /dev/null @@ -1 +0,0 @@ -property.to.load=somevalue \ No newline at end of file diff --git a/org.eclipse.m2e.jdt.tests/src/org/eclipse/m2e/jdt/tests/UnitTestLaunchConfigConfigurationTest.java b/org.eclipse.m2e.jdt.tests/src/org/eclipse/m2e/jdt/tests/UnitTestLaunchConfigConfigurationTest.java index ddee3485e1..e3f24fcc82 100644 --- a/org.eclipse.m2e.jdt.tests/src/org/eclipse/m2e/jdt/tests/UnitTestLaunchConfigConfigurationTest.java +++ b/org.eclipse.m2e.jdt.tests/src/org/eclipse/m2e/jdt/tests/UnitTestLaunchConfigConfigurationTest.java @@ -18,8 +18,8 @@ import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; -import java.util.Arrays; import java.util.Collection; +import java.util.List; import java.util.Map; import org.eclipse.core.resources.IFile; @@ -41,6 +41,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; import org.junit.runners.Parameterized.Parameters; @SuppressWarnings("restriction") @@ -50,47 +51,63 @@ public class UnitTestLaunchConfigConfigurationTest extends AbstractMavenProjectT private static final String REPLACED_SUREFIRE_POM_STRING = ""; private static final String REPLACED_FAILSAFE_POM_STRING = ""; private static final String ROOT_PATH = "/projects/surefireFailsafeToTestLaunchSettings"; - - private String testType; - private ILaunchManager launchManager; - - public UnitTestLaunchConfigConfigurationTest(String testType) { - super(); - this.testType = testType; - } + private static ILaunchManager LAUNCH_MANAGER = DebugPlugin.getDefault().getLaunchManager(); + + private static final String SUREFIRE_ARGS_SET = """ + + + --argLineItem=surefireArgLineValue + + + surefireProp1Value + + + surefireEnvironmentVariables1Value + + + """; + private static final String FAILSAFE_ARGS_SET = """ + + + --argLineItem=failsafeArgLineValue + + + failsafeProp1Value + + + failsafeEnvironmentVariables1Value + + + """; // Define the parameters to be used in the test @Parameters - public static Collection data() { - return Arrays.asList(new Object[][] { - { MavenRuntimeClasspathProvider.JDT_TESTNG_TEST }, - { MavenRuntimeClasspathProvider.JDT_JUNIT_TEST } }); + public static Collection data() { + return List.of(MavenRuntimeClasspathProvider.JDT_TESTNG_TEST, MavenRuntimeClasspathProvider.JDT_JUNIT_TEST); } + @Parameter(0) + public String testType; + @Override @Before public void setUp() throws Exception { super.setUp(); - ((MavenConfigurationImpl) MavenPlugin.getMavenConfiguration()).setAutomaticallyUpdateConfiguration(true); setAutoBuilding(true); - - // Get the launch manager - launchManager = DebugPlugin.getDefault().getLaunchManager(); - } @Test public void test_configuration_must_be_updated_with_surefire_config() throws CoreException, IOException, InterruptedException { // Get launch type - ILaunchConfigurationType type = launchManager.getLaunchConfigurationType(testType); + ILaunchConfigurationType type = LAUNCH_MANAGER.getLaunchConfigurationType(testType); assumeTrue(testType + " support not available", type != null); File pomFile = getTestFile("argumentsAreSet/pom.xml"); - File surefireConf = getTestFile("argumentsAreSet/surefireArgsSet.xml"); - File failsafeConf = null; + String surefireConf = SUREFIRE_ARGS_SET; + String failsafeConf = null; IProject project = importProject(pomFile.getAbsolutePath()); @@ -100,14 +117,14 @@ public void test_configuration_must_be_updated_with_surefire_config() updateProject(project); waitForJobsToComplete(); - ILaunchConfiguration[] updatedConfigurations = launchManager.getLaunchConfigurations(type); + ILaunchConfiguration[] updatedConfigurations = LAUNCH_MANAGER.getLaunchConfigurations(type); assertTrue(updatedConfigurations.length == 1); mergePomAndPluginConfigIntoProject(project, pomFile, surefireConf, failsafeConf); updateProject(project); waitForJobsToComplete(); - updatedConfigurations = launchManager.getLaunchConfigurations(type); + updatedConfigurations = LAUNCH_MANAGER.getLaunchConfigurations(type); assertTrue(updatedConfigurations.length == 1); ILaunchConfiguration config = updatedConfigurations[0]; @@ -133,13 +150,13 @@ public void test_configuration_must_be_updated_with_surefire_config() public void test_configuration_must_be_updated_with_failsafe_config() throws CoreException, IOException, InterruptedException { // Get launch type - ILaunchConfigurationType type = launchManager.getLaunchConfigurationType(testType); + ILaunchConfigurationType type = LAUNCH_MANAGER.getLaunchConfigurationType(testType); assumeTrue(testType + " support not available", type != null); File pomFile = getTestFile("argumentsAreSet/pom.xml"); - File surefireConf = null; - File failsafeConf = getTestFile("argumentsAreSet/failsafeArgsSet.xml"); + String surefireConf = null; + String failsafeConf = FAILSAFE_ARGS_SET; IProject project = importProject(pomFile.getAbsolutePath()); // waitForJobsToComplete(); @@ -150,13 +167,13 @@ public void test_configuration_must_be_updated_with_failsafe_config() updateProject(project); waitForJobsToComplete(); - ILaunchConfiguration[] updatedConfigurations = launchManager.getLaunchConfigurations(type); + ILaunchConfiguration[] updatedConfigurations = LAUNCH_MANAGER.getLaunchConfigurations(type); assertTrue(updatedConfigurations.length == 1); mergePomAndPluginConfigIntoProject(project, pomFile, surefireConf, failsafeConf); updateProject(project); - updatedConfigurations = launchManager.getLaunchConfigurations(type); + updatedConfigurations = LAUNCH_MANAGER.getLaunchConfigurations(type); assertTrue(updatedConfigurations.length == 1); ILaunchConfiguration config = updatedConfigurations[0]; @@ -182,13 +199,13 @@ public void test_configuration_must_be_updated_with_failsafe_config() public void test_configuration_must_be_updated_with_surefire_config_when_created() throws CoreException, IOException, InterruptedException { // Get launch type - ILaunchConfigurationType type = launchManager.getLaunchConfigurationType(testType); + ILaunchConfigurationType type = LAUNCH_MANAGER.getLaunchConfigurationType(testType); assumeTrue(testType + " support not available", type != null); File pomFile = getTestFile("argumentsAreSet/pom.xml"); - File surefireConf = getTestFile("argumentsAreSet/surefireArgsSet.xml"); - File failsafeConf = null; + String surefireConf = SUREFIRE_ARGS_SET; + String failsafeConf = null; IProject project = importProject(pomFile.getAbsolutePath()); mergePomAndPluginConfigIntoProject(project, pomFile, surefireConf, failsafeConf); @@ -198,7 +215,7 @@ public void test_configuration_must_be_updated_with_surefire_config_when_created // create basic unit test createDefaultTest(project, type, "test.SomeTest"); - ILaunchConfiguration[] updatedConfigurations = launchManager.getLaunchConfigurations(type); + ILaunchConfiguration[] updatedConfigurations = LAUNCH_MANAGER.getLaunchConfigurations(type); assertTrue(updatedConfigurations.length == 1); ILaunchConfiguration config = updatedConfigurations[0]; @@ -224,13 +241,13 @@ public void test_configuration_must_be_updated_with_surefire_config_when_created public void test_configuration_must_be_updated_with_failSafe_config_when_created() throws CoreException, IOException, InterruptedException { // Get launch type - ILaunchConfigurationType type = launchManager.getLaunchConfigurationType(testType); + ILaunchConfigurationType type = LAUNCH_MANAGER.getLaunchConfigurationType(testType); assumeTrue(testType + " support not available", type != null); File pomFile = getTestFile("argumentsAreSet/pom.xml"); - File surefireConf = null; - File failsafeConf = getTestFile("argumentsAreSet/failsafeArgsSet.xml"); + String surefireConf = null; + String failsafeConf = FAILSAFE_ARGS_SET; IProject project = importProject(pomFile.getAbsolutePath()); mergePomAndPluginConfigIntoProject(project, pomFile, surefireConf, failsafeConf); @@ -240,7 +257,7 @@ public void test_configuration_must_be_updated_with_failSafe_config_when_created // create basic unit test createDefaultTest(project, type, "test.SomeTestIT"); - ILaunchConfiguration[] updatedConfigurations = launchManager.getLaunchConfigurations(type); + ILaunchConfiguration[] updatedConfigurations = LAUNCH_MANAGER.getLaunchConfigurations(type); assertTrue(updatedConfigurations.length == 1); ILaunchConfiguration config = updatedConfigurations[0]; @@ -279,20 +296,18 @@ private void createDefaultTest(IProject project, ILaunchConfigurationType type, } // Merge the pom and plugins configuration into the project - private void mergePomAndPluginConfigIntoProject(IProject project, File pomTemplate, File surefireConf, - File failsafeConf) throws IOException, CoreException { + private void mergePomAndPluginConfigIntoProject(IProject project, File pomTemplate, String surefireConfiguration, + String failsafeConfiguration) throws IOException, CoreException { String pom = Utils.read(project, pomTemplate); IFile pomFileWS = project.getFile(pomTemplate.getName()); String newContent = pom; - if (surefireConf != null) { - String plugin = Utils.read(project, surefireConf); - newContent = newContent.replace(REPLACED_SUREFIRE_POM_STRING, plugin); + if (surefireConfiguration != null) { + newContent = newContent.replace(REPLACED_SUREFIRE_POM_STRING, surefireConfiguration); } - if (failsafeConf != null) { - String plugin = Utils.read(project, failsafeConf); - newContent = newContent.replace(REPLACED_FAILSAFE_POM_STRING, plugin); + if (failsafeConfiguration != null) { + newContent = newContent.replace(REPLACED_FAILSAFE_POM_STRING, failsafeConfiguration); } pomFileWS.setContents(new ByteArrayInputStream(newContent.getBytes()), true, false, null); diff --git a/org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/UnitTestSupport.java b/org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/UnitTestSupport.java index fa62c72939..50faddaa9c 100644 --- a/org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/UnitTestSupport.java +++ b/org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/UnitTestSupport.java @@ -19,12 +19,13 @@ import java.lang.reflect.Method; import java.nio.file.Files; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Objects; import java.util.Optional; import java.util.Set; +import java.util.StringJoiner; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,7 +38,6 @@ import org.eclipse.debug.core.ILaunchConfigurationType; import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; import org.eclipse.debug.core.ILaunchManager; -import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; import org.eclipse.jdt.launching.JavaRuntime; @@ -63,10 +63,7 @@ public class UnitTestSupport { private static final boolean FEATURE_ENABLED = Boolean .parseBoolean(System.getProperty("m2e.process.test.configuration", "true")); - /** - * Logger - */ - private static final Logger log = LoggerFactory.getLogger(UnitTestSupport.class); + private static final Logger LOG = LoggerFactory.getLogger(UnitTestSupport.class); /** * org.apache.maven.surefire.api.testset.TestListResolver method name to check if a test should run @@ -153,32 +150,20 @@ public class UnitTestSupport { */ private static final String MAVEN_PLUGIN_GROUP_ID = "org.apache.maven.plugins"; - /** - * List of test executions - */ private static final List TEST_EXECUTIONS = List.of( new ExecutionId(MAVEN_PLUGIN_GROUP_ID, SUREFIRE_PLUGIN_ARTIFACT_ID, GOAL_TEST), new ExecutionId(MAVEN_PLUGIN_GROUP_ID, FAILSAFE_PLUGIN_ARTIFACT_ID, GOAL_INTEGRATION_TEST)); - /** - * Supported launch types - */ private static final Set CONSIDERED_LAUNCH_TYPES = Set.of(MavenRuntimeClasspathProvider.JDT_JUNIT_TEST, MavenRuntimeClasspathProvider.JDT_TESTNG_TEST); /** * Reset all launch configurations for the project - * - * @param project the project */ public static void resetLaunchConfigurations(IProject project) { - - if(!FEATURE_ENABLED) { - return; + if(FEATURE_ENABLED) { + new ConfigurationManager().setupLaunchConfigurations(project); } - - new ConfigurationManager().setupLaunchConfigurations(project); - } /** @@ -187,13 +172,9 @@ public static void resetLaunchConfigurations(IProject project) { * @param configuration the configuration */ public static void setupLaunchConfigurationFromMavenConfiguration(ILaunchConfiguration configuration) { - - if(!FEATURE_ENABLED) { - return; + if(FEATURE_ENABLED) { + new ConfigurationManager().setupLaunchConfiguration(configuration); } - - new ConfigurationManager().setupLaunchConfiguration(configuration); - } /** @@ -206,56 +187,35 @@ private static boolean isSupportedType(String id) { return id != null && CONSIDERED_LAUNCH_TYPES.contains(id); } - /** - * Configuration manager - */ private static class ConfigurationManager { - /** - * Execution cache - */ - private MavenCache cache = new MavenCache(); - /** * Reset all launch configurations for the project - * - * @param project the project */ public void setupLaunchConfigurations(IProject project) { if(project != null && project.exists()) { - - // Get the launch manager ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager(); - for(String launchTypeId : CONSIDERED_LAUNCH_TYPES) { try { // Get launch type ILaunchConfigurationType type = launchManager.getLaunchConfigurationType(launchTypeId); - if(type == null) { // unknown type, probably support not installed continue; } - // Get all launch configurations for the type ILaunchConfiguration[] configurations = launchManager.getLaunchConfigurations(type); - - // Process each launch configuration for(ILaunchConfiguration configuration : configurations) { - - // configuration project name - String confProjectName = configuration.getAttribute(LAUNCH_CONFIG_PROJECT, ""); - String projectName = project.getName(); - - // Check if the configuration is associated with the desired project and type - if(projectName.equals(confProjectName)) { - log.info("Reset launch configuration name: {}", configuration.getName()); + // Check if the configuration is associated with the desired project and type + String configurationProjectName = configuration.getAttribute(LAUNCH_CONFIG_PROJECT, ""); + if(project.getName().equals(configurationProjectName)) { + LOG.info("Reset launch configuration name: {}", configuration.getName()); setupLaunchConfiguration(configuration); } } } catch(Exception e) { - log.error(e.getMessage(), e); + LOG.error(e.getMessage(), e); } } @@ -264,24 +224,19 @@ public void setupLaunchConfigurations(IProject project) { /** * Reset the launch configuration - * - * @param configuration the configuration */ public void setupLaunchConfiguration(ILaunchConfiguration configuration) { - try { - if(!isSupportedType(configuration.getType().getIdentifier())) { return; } - - IJavaProject javaProject = JavaRuntime.getJavaProject(configuration); - IProject project = javaProject.getProject(); + IProject project = JavaRuntime.getJavaProject(configuration).getProject(); // maven project if project has a maven classpath boolean isMavenProject = configuration .getAttribute(IJavaLaunchConfigurationConstants.ATTR_CLASSPATH_PROVIDER, "") .equals(MavenRuntimeClasspathProvider.MAVEN_CLASSPATH_PROVIDER); + //TODO: why not check the nature?! if(project != null && project.hasNature(IMavenConstants.NATURE_ID) && isMavenProject) { @@ -289,7 +244,7 @@ public void setupLaunchConfiguration(ILaunchConfiguration configuration) { case MavenRuntimeClasspathProvider.JDT_TESTNG_TEST: case MavenRuntimeClasspathProvider.JDT_JUNIT_TEST: { - log.info("Updating {} from maven configuration", configuration.getName()); + LOG.info("Updating {} from maven configuration", configuration.getName()); IMavenProjectFacade facade = MavenPlugin.getMavenProjectRegistry().getProject(project); @@ -304,36 +259,28 @@ public void setupLaunchConfiguration(ILaunchConfiguration configuration) { } } } catch(CoreException ex) { - log.error(ex.getMessage(), ex); + LOG.error(ex.getMessage(), ex); } } /** * Define the configuration values - * - * @param project the project - * @param configuration the configuration - * @param args the arguments - * @throws CoreException */ private void defineConfigurationValues(IProject project, ILaunchConfiguration configuration, TestLaunchArguments args) throws CoreException { - ILaunchConfigurationWorkingCopy copy = configuration.getWorkingCopy(); - StringBuilder launchArguments = new StringBuilder(); + ILaunchConfigurationWorkingCopy copy = configuration.getWorkingCopy(); + StringJoiner launchArguments = new StringJoiner("\n"); if(args.enableAssertions()) { - launchArguments.append("-ea").append("\n"); + launchArguments.add("-ea"); } - if(args.argLine() != null) { - launchArguments.append(args.argLine()).append("\n"); + launchArguments.add(args.argLine()); } if(args.systemPropertyVariables() != null) { - args.systemPropertyVariables().entrySet().forEach( - e -> launchArguments.append("-D").append(e.getKey()).append("=").append(e.getValue()).append("\n")); + args.systemPropertyVariables().forEach((key, value) -> launchArguments.add("-D" + key + "=" + value)); } - copy.setAttribute(LAUNCH_CONFIG_VM_ARGUMENTS, launchArguments.toString()); try { @@ -344,7 +291,7 @@ private void defineConfigurationValues(IProject project, ILaunchConfiguration co copy.setAttribute(LAUNCH_CONFIG_WORKING_DIRECTORY, (String) null); } } catch(IOException ex) { - log.error(ex.getMessage(), ex); + LOG.error(ex.getMessage(), ex); } if(args.environmentVariables() != null) { @@ -358,7 +305,6 @@ private TestLaunchArguments getTestLaunchArguments(ILaunchConfiguration configur IProgressMonitor monitor) throws CoreException { String testClass = configuration.getAttribute(LAUNCH_CONFIG_MAIN_CLASS, ""); - MavenProject mavenProject = facade.getMavenProject(); // find test executions @@ -375,86 +321,80 @@ private TestLaunchArguments getTestLaunchArguments(ILaunchConfiguration configur for(MojoExecution execution : handlers) { return getTestLaunchArguments(mavenProject, execution, monitor); } - return null; } /** * Check if the test is handled by the plugin * - * @param facade the maven project facade - * @param monitor the progress monitor - * @param execution the plugin execution - * @param testClass the test class * @return true if the test is handled by the plugin */ - private boolean isTestHandledByPlugin(IMavenProjectFacade facade, IProgressMonitor monitor, MojoExecution execution, String testClass) { String testFile = testClass.replace(".", "/") + ".class"; // get a configured mojo instance - Mojo mojo = cache.getMojoInstance(facade, execution, monitor); - + Optional mojo = getMojoInstance(facade, execution, monitor); // get an instance of org.apache.maven.surefire.api.testset.TestListResolver directly from the plugin instance - Optional testResolverInstance = Optional.of(mojo) - .map(o -> findMethod(o, GET_INCLUDED_AND_EXCLUDED_TESTS_METHOD)).map(m -> { - m.setAccessible(true); - return m; - }).map(m -> uncheckedInvoke(m, mojo)); - + Optional testResolverInstance = mojo.map(o -> uncheckedInvoke(o, GET_INCLUDED_AND_EXCLUDED_TESTS_METHOD)); // check if the test is handled by the plugin - Boolean isTestHandled = testResolverInstance.filter(Objects::nonNull) - .map(o -> findMethod(o, SHOULD_RUN_METHOD, String.class, String.class)).map(m -> { - m.setAccessible(true); - return m; - }).map(m -> uncheckedInvoke(m, testResolverInstance.get(), testFile, "")).map(Boolean.class::cast) - .orElse(false); + Boolean isTestHandled = testResolverInstance.map(o -> uncheckedInvoke(o, SHOULD_RUN_METHOD, testFile, "")) + .map(Boolean.class::cast).orElse(false); return isTestHandled; } /** - * Lookup method in class and ancestors + * Invoke a method without throwing checked exceptions * - * @param instance the instance on which we search the method - * @return a method or null if not found + * @param instance the instance on which to invoke the method + * @param methodName the name of the method to invoke + * @param arguments the arguments + * @return the result of the invocation */ - private Method findMethod(Object instance, String methodName, Class... parameters) { - Method method = null; + private static Object uncheckedInvoke(Object instance, String methodName, Object... arguments) { Class searchClass = instance.getClass(); - - while(method == null && searchClass != null) { + Class[] parameters = Arrays.stream(arguments).map(p -> p.getClass()).toArray(Class[]::new); + while(searchClass != null) { try { - method = searchClass.getDeclaredMethod(methodName, parameters); + Method method = searchClass.getDeclaredMethod(methodName, parameters); + if(!method.trySetAccessible()) { + LOG.error("Cannot make accessible {}", method); + } else { + return method.invoke(instance, arguments); + } } catch(NoSuchMethodException | SecurityException ex) { - log.debug(ex.getMessage(), ex); + LOG.debug(ex.getMessage(), ex); + } catch(IllegalAccessException | InvocationTargetException ex) { + LOG.error(ex.getMessage(), ex); } searchClass = searchClass.getSuperclass(); } - - return method; + return null; } + /** Execution cache */ + private final Map mojoCache = new HashMap<>(); + /** - * Invoke a method without throwing checked exceptions - * - * @param method the method to invoke - * @param instance the instance on which to invoke the method - * @param parameters the parameters - * @return the result of the invocation + * Get a configured mojo instance */ - private static Object uncheckedInvoke(Method method, Object instance, Object... parameters) { - try { - return method.invoke(instance, parameters); - } catch(IllegalAccessException | InvocationTargetException ex) { - log.error(ex.getMessage(), ex); - } - return null; + public Optional getMojoInstance(IMavenProjectFacade facade, MojoExecution execution, + IProgressMonitor monitor) { + return Optional.ofNullable(mojoCache.computeIfAbsent(execution, exe -> { + try { + return facade.createExecutionContext().execute(facade.getMavenProject(), + (context, pm) -> MavenPlugin.getMaven().getConfiguredMojo(context.getSession(), exe, Mojo.class), + monitor); + } catch(CoreException ex) { + LOG.error("Unable to instanciate mojo instance", ex); + return null; + } + })); } /** - * get all the arguments provided to the plugin for the provided {@link MojoExecution} + * Get all the arguments provided to the plugin for the provided {@link MojoExecution}. * * @param mavenProject the current maven project * @param execution the plugin execution @@ -464,10 +404,8 @@ private static Object uncheckedInvoke(Method method, Object instance, Object... @SuppressWarnings("unchecked") private TestLaunchArguments getTestLaunchArguments(MavenProject mavenProject, MojoExecution execution, IProgressMonitor monitor) { - - IMaven maven = MavenPlugin.getMaven(); - try { + IMaven maven = MavenPlugin.getMaven(); return new TestLaunchArguments( maven.getMojoParameterValue(mavenProject, execution, PLUGIN_ARGLINE, String.class, monitor), maven.getMojoParameterValue(mavenProject, execution, PLUGIN_SYSPROP_VARIABLES, Map.class, monitor), @@ -475,55 +413,13 @@ private TestLaunchArguments getTestLaunchArguments(MavenProject mavenProject, Mo maven.getMojoParameterValue(mavenProject, execution, PLUGIN_WORKING_DIRECTORY, File.class, monitor), maven.getMojoParameterValue(mavenProject, execution, PLUGIN_ENABLE_ASSERTIONS, Boolean.class, monitor)); } catch(Exception e) { - log.error(e.getMessage(), e); + LOG.error(e.getMessage(), e); } return null; } } - /** - * Cache for maven executions - */ - private static class MavenCache { - - /** - * Cache for the mojos - */ - Map mojoCache = new HashMap<>(); - - /** - * Get a configured mojo instance - * - * @param facade the maven project facade - * @param mojoExecution the mojo execution - * @param monitor the progress monitor - * @return the mojo instance - */ - public Mojo getMojoInstance(IMavenProjectFacade facade, MojoExecution mojoExecution, IProgressMonitor monitor) { - - if(mojoCache.containsKey(mojoExecution)) { - return mojoCache.get(mojoExecution); - } - - IMaven maven = MavenPlugin.getMaven(); - - try { - Mojo mojo = facade.createExecutionContext().execute(facade.getMavenProject(), (context, pm) -> { - return maven.getConfiguredMojo(context.getSession(), mojoExecution, Mojo.class); - }, monitor); - - mojoCache.put(mojoExecution, mojo); - - return mojo; - - } catch(CoreException ex) { - log.error("Unable to instanciate mojo instance", ex); - } - return null; - } - } - /** * Holder for the surefire/failsafe launch arguments */ diff --git a/products/m2e-ide/Eclipse-M2E-IDE.launch b/products/m2e-ide/Eclipse-M2E-IDE.launch index 571d0a92b6..32763b41c8 100644 --- a/products/m2e-ide/Eclipse-M2E-IDE.launch +++ b/products/m2e-ide/Eclipse-M2E-IDE.launch @@ -1,13 +1,13 @@ - + - - - - - + + + + + @@ -28,7 +28,7 @@ - +