DeployLifecycleParticipant extends AbstractMavenLifecycleParticipant implements LogEnabled { public void afterProjectsRead(final MavenSession session) throws MavenExecutionException { try { final int totalModules = session.getProjects().size(); logger.info("Inspecting build with total of " + totalModules + " modules..."); int stagingGoalsFoundInModules = 0; for (MavenProject project : session.getProjects()) { final Plugin nexusMavenPlugin = getBuildPluginsNexusMavenPlugin(project.getModel()); if (nexusMavenPlugin != null) { if (!nexusMavenPlugin.getExecutions().isEmpty()) { for (PluginExecution pluginExecution : nexusMavenPlugin.getExecutions()) { final List<String> goals = pluginExecution.getGoals(); if (goals.contains("deploy") || goals.contains("deploy-staged") || goals.contains("staging-close") || goals.contains("staging-release") || goals.contains("staging-promote")) { stagingGoalsFoundInModules++; break; } } } } } if (stagingGoalsFoundInModules > 0) { logger.info("Not installing Nexus Staging features:"); if (stagingGoalsFoundInModules > 0) { logger.info(" * Preexisting staging related goal bindings found in " + stagingGoalsFoundInModules + " modules."); } return; } logger.info("Installing Nexus Staging features:"); int skipped = 0; for (MavenProject project : session.getProjects()) { final Plugin nexusMavenPlugin = getBuildPluginsNexusMavenPlugin(project.getModel()); if (nexusMavenPlugin != null) { final Plugin mavenDeployPlugin = getBuildPluginsMavenDeployPlugin(project.getModel()); if (mavenDeployPlugin != null) { mavenDeployPlugin.getExecutions().clear(); final PluginExecution execution = new PluginExecution(); execution.setId("injected-nexus-deploy"); execution.getGoals().add("deploy"); execution.setPhase("deploy"); execution.setConfiguration(nexusMavenPlugin.getConfiguration()); nexusMavenPlugin.getExecutions().add(execution); skipped++; } } } if (skipped > 0) { logger.info(" ... total of " + skipped + " executions of maven-deploy-plugin replaced with " + getPluginArtifactId()); } } catch (IllegalStateException e) { throw new MavenExecutionException(e.getMessage(), e); } }  @Override void enableLogging(final Logger logger); void afterProjectsRead(final MavenSession session); static String _THIS_GROUP_ID; static String _THIS_ARTIFACT_ID; static String MAVEN_DEPLOY_PLUGIN_GROUP_ID; static String MAVEN_DEPLOY_PLUGIN_ARTIFACT_ID; }
@Test public void testSimpleOneModuleWithManuallySetPlugin() throws MavenExecutionException { final Model model = createModel("org.foo", "simple-but-manually-set"); model.getBuild().getPlugins().add( createPlugin(DeployLifecycleParticipant.MAVEN_DEPLOY_PLUGIN_GROUP_ID, DeployLifecycleParticipant.MAVEN_DEPLOY_PLUGIN_ARTIFACT_ID, "deploy")); model.getBuild().getPlugins().add( createPlugin(DeployLifecycleParticipant._THIS_GROUP_ID, DeployLifecycleParticipant._THIS_ARTIFACT_ID, "deploy")); final MavenSession mockSession = createSessionWithProjectsForModels(model); defaultLifecycleParticipant.afterProjectsRead(mockSession); for (Plugin plugin : mockSession.getProjects().get(0).getModel().getBuild().getPlugins()) { if (DeployLifecycleParticipant.MAVEN_DEPLOY_PLUGIN_ARTIFACT_ID.equals(plugin.getArtifactId())) { Assert.assertEquals("One execution for plugin " + DeployLifecycleParticipant.MAVEN_DEPLOY_PLUGIN_ARTIFACT_ID, 1, plugin.getExecutions().size()); } else if (DeployLifecycleParticipant._THIS_ARTIFACT_ID.equals(plugin.getArtifactId())) { Assert.assertEquals("One execution for plugin " + DeployLifecycleParticipant._THIS_ARTIFACT_ID, 1, plugin.getExecutions().size()); } else { Assert.fail("Unknown plugin (): " + plugin.getGroupId() + ":" + plugin.getArtifactId()); } } Assert.assertTrue("Missing log?", fakeLogger.getLoggedStuff().contains("Not installing Nexus Staging")); }
@Test public void testComplexReactorWithAggregators() throws MavenExecutionException { final Model aggregatorPom = createModel("org.foo", "aggregator"); final Model parentA = createModel("org.foo", "parent-a"); parentA.getBuild().getPlugins().add( createPlugin(DeployLifecycleParticipant.MAVEN_DEPLOY_PLUGIN_GROUP_ID, DeployLifecycleParticipant.MAVEN_DEPLOY_PLUGIN_ARTIFACT_ID, "deploy")); parentA.getBuild().getPlugins().add( createPluginAndSetConfig(DeployLifecycleParticipant._THIS_GROUP_ID, DeployLifecycleParticipant._THIS_ARTIFACT_ID, null, "profile-a")); final Model moduleA = createModel("org.foo", "module-a"); moduleA.getBuild().getPlugins().add( createPlugin(DeployLifecycleParticipant.MAVEN_DEPLOY_PLUGIN_GROUP_ID, DeployLifecycleParticipant.MAVEN_DEPLOY_PLUGIN_ARTIFACT_ID, "deploy")); moduleA.getBuild().getPlugins().add( createPluginAndSetConfig(DeployLifecycleParticipant._THIS_GROUP_ID, DeployLifecycleParticipant._THIS_ARTIFACT_ID, null, "profile-a")); final Model parentB = createModel("org.foo", "parent-b"); parentB.getBuild().getPlugins().add( createPlugin(DeployLifecycleParticipant.MAVEN_DEPLOY_PLUGIN_GROUP_ID, DeployLifecycleParticipant.MAVEN_DEPLOY_PLUGIN_ARTIFACT_ID, "deploy")); parentB.getBuild().getPlugins().add( createPluginAndSetConfig(DeployLifecycleParticipant._THIS_GROUP_ID, DeployLifecycleParticipant._THIS_ARTIFACT_ID, null, "profile-b")); final Model moduleB = createModel("org.foo", "module-b"); moduleB.getBuild().getPlugins().add( createPlugin(DeployLifecycleParticipant.MAVEN_DEPLOY_PLUGIN_GROUP_ID, DeployLifecycleParticipant.MAVEN_DEPLOY_PLUGIN_ARTIFACT_ID, "deploy")); moduleB.getBuild().getPlugins().add( createPluginAndSetConfig(DeployLifecycleParticipant._THIS_GROUP_ID, DeployLifecycleParticipant._THIS_ARTIFACT_ID, null, "profile-b")); final MavenSession mockSession = createSessionWithProjectsForModels(aggregatorPom, parentA, moduleA, parentB, moduleB); defaultLifecycleParticipant.afterProjectsRead(mockSession); for (Plugin plugin : mockSession.getProjects().get(0).getModel().getBuild().getPlugins()) { if (DeployLifecycleParticipant.MAVEN_DEPLOY_PLUGIN_ARTIFACT_ID.equals(plugin.getArtifactId())) { Assert.assertTrue("No executions for plugin " + DeployLifecycleParticipant.MAVEN_DEPLOY_PLUGIN_ARTIFACT_ID, plugin.getExecutions().isEmpty()); } else if (DeployLifecycleParticipant._THIS_ARTIFACT_ID.equals(plugin.getArtifactId())) { Assert.assertEquals("One execution for plugin " + DeployLifecycleParticipant._THIS_ARTIFACT_ID, 1, plugin.getExecutions().size()); if (plugin.getArtifactId().endsWith("-a")) { Assert.assertTrue("Config mismatch!", ((Xpp3Dom) plugin.getConfiguration()).getChild("aSwitch").getValue().endsWith("-a")); } else if (plugin.getArtifactId().endsWith("-b")) { Assert.assertTrue("Config mismatch!", ((Xpp3Dom) plugin.getConfiguration()).getChild("aSwitch").getValue().endsWith("-b")); } else { Assert.fail("Unknown plugin config: " + plugin.getGroupId() + ":" + plugin.getArtifactId()); } } else { Assert.fail("Unknown plugin: " + plugin.getGroupId() + ":" + plugin.getArtifactId()); } } Assert.assertTrue("Missing log?", fakeLogger.getLoggedStuff().contains("Installing Nexus Staging")); Assert.assertTrue("Wrong count reported?", fakeLogger.getLoggedStuff().contains("... total of 4")); }
@Test public void testABadProject() throws MavenExecutionException { final Model model = createModel("org.foo", "bad"); model.getBuild().getPlugins().add( createPlugin(DeployLifecycleParticipant.MAVEN_DEPLOY_PLUGIN_GROUP_ID, DeployLifecycleParticipant.MAVEN_DEPLOY_PLUGIN_ARTIFACT_ID, "deploy")); model.getBuild().getPlugins().add( createPluginAndSetConfig(DeployLifecycleParticipant._THIS_GROUP_ID, DeployLifecycleParticipant._THIS_ARTIFACT_ID, null, "profile-a")); model.getBuild().getPlugins().add( createPluginAndSetConfig(DeployLifecycleParticipant._THIS_GROUP_ID, DeployLifecycleParticipant._THIS_ARTIFACT_ID, null, "profile-a")); final MavenSession mockSession = createSessionWithProjectsForModels(model); try { defaultLifecycleParticipant.afterProjectsRead(mockSession); Assert.fail("Should choke on this!"); } catch (MavenExecutionException e) { Assert.assertEquals("The build contains multiple versions of plugin " + DeployLifecycleParticipant._THIS_GROUP_ID + ":" + DeployLifecycleParticipant._THIS_ARTIFACT_ID, e.getMessage()); } }
@Test public void testSimpleOneModule() throws MavenExecutionException { final Model model = createModel("org.foo", "simple"); model.getBuild().getPlugins().add( createPlugin(DeployLifecycleParticipant.MAVEN_DEPLOY_PLUGIN_GROUP_ID, DeployLifecycleParticipant.MAVEN_DEPLOY_PLUGIN_ARTIFACT_ID, "deploy")); model.getBuild().getPlugins().add( createPlugin(DeployLifecycleParticipant._THIS_GROUP_ID, DeployLifecycleParticipant._THIS_ARTIFACT_ID)); final MavenSession mockSession = createSessionWithProjectsForModels(model); defaultLifecycleParticipant.afterProjectsRead(mockSession); for (Plugin plugin : mockSession.getProjects().get(0).getModel().getBuild().getPlugins()) { if (DeployLifecycleParticipant.MAVEN_DEPLOY_PLUGIN_ARTIFACT_ID.equals(plugin.getArtifactId())) { Assert.assertTrue("No executions for plugin " + DeployLifecycleParticipant.MAVEN_DEPLOY_PLUGIN_ARTIFACT_ID, plugin.getExecutions().isEmpty()); } else if (DeployLifecycleParticipant._THIS_ARTIFACT_ID.equals(plugin.getArtifactId())) { Assert.assertEquals("One execution for plugin " + DeployLifecycleParticipant._THIS_ARTIFACT_ID, 1, plugin.getExecutions().size()); } else { Assert.fail("Unknown plugin (): " + plugin.getGroupId() + ":" + plugin.getArtifactId()); } } Assert.assertTrue("Missing log?", fakeLogger.getLoggedStuff().contains("Installing Nexus Staging")); Assert.assertTrue("Wrong count reported?", fakeLogger.getLoggedStuff().contains("... total of 1")); }