private void parseLifecyclePhaseDefinitions( Map<Plugin, Plugin> plugins, String phase, LifecyclePhase goals) { List<LifecycleMojo> mojos = goals.getMojos(); if (mojos != null) { for (int i = 0; i < mojos.size(); i++) { LifecycleMojo mojo = mojos.get(i); GoalSpec gs = parseGoalSpec(mojo.getGoal()); if (gs == null) { logger.warn( "Ignored invalid goal specification '" + mojo.getGoal() + "' from lifecycle mapping for phase " + phase); continue; } Plugin plugin = new Plugin(); plugin.setGroupId(gs.groupId); plugin.setArtifactId(gs.artifactId); plugin.setVersion(gs.version); Plugin existing = plugins.get(plugin); if (existing != null) { if (existing.getVersion() == null) { existing.setVersion(plugin.getVersion()); } plugin = existing; } else { plugins.put(plugin, plugin); } PluginExecution execution = new PluginExecution(); execution.setId(getExecutionId(plugin, gs.goal)); execution.setPhase(phase); execution.setPriority(i - mojos.size()); execution.getGoals().add(gs.goal); execution.setConfiguration(mojo.getConfiguration()); plugin.setDependencies(mojo.getDependencies()); plugin.getExecutions().add(execution); } } }
private static String getKey(Plugin plugin, boolean extension) { String version = ArtifactUtils.toSnapshotVersion(plugin.getVersion()); return (extension ? "extension>" : "plugin>") + plugin.getGroupId() + ":" + plugin.getArtifactId() + ":" + version; }
private List<String> alterModel(MavenProject project, String newVersion) { Model originalModel = project.getOriginalModel(); originalModel.setVersion(newVersion); List<String> errors = new ArrayList<String>(); String searchingFrom = project.getArtifactId(); MavenProject parent = project.getParent(); if (parent != null && isSnapshot(parent.getVersion())) { try { ReleasableModule parentBeingReleased = reactor.find(parent.getGroupId(), parent.getArtifactId(), parent.getVersion()); originalModel.getParent().setVersion(parentBeingReleased.getVersionToDependOn()); log.debug( " Parent " + parentBeingReleased.getArtifactId() + " rewritten to version " + parentBeingReleased.getVersionToDependOn()); } catch (UnresolvedSnapshotDependencyException e) { errors.add("The parent of " + searchingFrom + " is " + e.artifactId + " " + e.version); } } for (Dependency dependency : originalModel.getDependencies()) { String version = dependency.getVersion(); if (isSnapshot(version)) { try { ReleasableModule dependencyBeingReleased = reactor.find(dependency.getGroupId(), dependency.getArtifactId(), version); dependency.setVersion(dependencyBeingReleased.getVersionToDependOn()); log.debug( " Dependency on " + dependencyBeingReleased.getArtifactId() + " rewritten to version " + dependencyBeingReleased.getVersionToDependOn()); } catch (UnresolvedSnapshotDependencyException e) { errors.add(searchingFrom + " references dependency " + e.artifactId + " " + e.version); } } else log.debug( " Dependency on " + dependency.getArtifactId() + " kept at version " + dependency.getVersion()); } for (Plugin plugin : project.getModel().getBuild().getPlugins()) { String version = plugin.getVersion(); if (isSnapshot(version)) { if (!isMultiModuleReleasePlugin(plugin)) { errors.add( searchingFrom + " references plugin " + plugin.getArtifactId() + " " + version); } } } return errors; }
@Test public void registerNewPlugin() { MavenProject pom = MavenTestUtils.loadPom(getClass(), "registerNewPlugin.xml"); MavenPlugin mavenPlugin = MavenPlugin.registerPlugin(pom, "mygroup", "my.artifact", "1.0", true); assertThat(mavenPlugin).isNotNull(); Plugin plugin = MavenUtils.getPlugin(pom.getBuildPlugins(), "mygroup", "my.artifact"); assertThat(plugin).isNotNull(); assertThat(plugin.getVersion()).isEqualTo("1.0"); }
@Test public void keepPluginDependencies() { MavenProject pom = MavenTestUtils.loadPom(getClass(), "keepPluginDependencies.xml"); MavenPlugin mavenPlugin = MavenPlugin.registerPlugin(pom, "mygroup", "my.artifact", "1.0", false); assertThat(mavenPlugin).isNotNull(); Plugin plugin = MavenUtils.getPlugin(pom.getBuildPlugins(), "mygroup", "my.artifact"); assertThat(plugin).isNotNull(); assertThat(plugin.getVersion()).isEqualTo("0.9"); assertThat(plugin.getDependencies().size()).isEqualTo(1); }
@Override public void generate(final File reportsDir, final VersionManagerSession session) throws VManException { Map<VersionlessProjectKey, Set<Plugin>> missingPlugins = session.getUnmanagedPluginRefs(); if (missingPlugins.isEmpty()) { return; } Element plugins = new Element("plugins"); for (Map.Entry<VersionlessProjectKey, Set<Plugin>> pluginsEntry : missingPlugins.entrySet()) { if (plugins.getContentSize() > 0) { plugins.addContent("\n\n"); } plugins.addContent(new Comment("START: " + pluginsEntry.getKey())); for (Plugin dep : pluginsEntry.getValue()) { Element d = new Element("plugin"); plugins.addContent(d); d.addContent(new Element("groupId").setText(dep.getGroupId())); d.addContent(new Element("artifactId").setText(dep.getArtifactId())); d.addContent(new Element("version").setText(dep.getVersion())); } plugins.addContent(new Comment("END: " + pluginsEntry.getKey())); } Element build = new Element("build"); build.addContent(new Element("pluginManagement").setContent(plugins)); Document doc = new Document(build); Format fmt = Format.getPrettyFormat(); fmt.setIndent(" "); fmt.setTextMode(TextMode.PRESERVE); XMLOutputter output = new XMLOutputter(fmt); File report = new File(reportsDir, ID + ".xml"); FileWriter writer = null; try { reportsDir.mkdirs(); writer = new FileWriter(report); output.output(doc, writer); } catch (IOException e) { throw new VManException("Failed to generate %s report! Error: %s", e, ID, e.getMessage()); } finally { closeQuietly(writer); } }
// DefaultProjectBuilder public Artifact createPluginArtifact(Plugin plugin) { VersionRange versionRange; try { String version = plugin.getVersion(); if (StringUtils.isEmpty(version)) { version = "RELEASE"; } versionRange = VersionRange.createFromVersionSpec(version); } catch (InvalidVersionSpecificationException e) { return null; } return XcreatePluginArtifact(plugin.getGroupId(), plugin.getArtifactId(), versionRange); }
@Test public void doNotOverrideVersionFromPluginManagementSection() { MavenProject pom = MavenTestUtils.loadPom(getClass(), "overridePluginManagementSection.xml"); MavenPlugin mavenPlugin = MavenPlugin.registerPlugin(pom, "mygroup", "my.artifact", "1.0", false); assertThat(mavenPlugin).isNotNull(); Plugin plugin = MavenUtils.getPlugin(pom.getBuildPlugins(), "mygroup", "my.artifact"); assertThat(plugin).isNotNull(); assertThat(plugin.getVersion()).isEqualTo("0.9"); Plugin pluginManagement = MavenUtils.getPlugin(pom.getPluginManagement().getPlugins(), "mygroup", "my.artifact"); assertThat(pluginManagement).isNull(); }
@Override public List<MavenPlugin> listConfiguredPlugins() { MavenCoreFacet mavenCoreFacet = project.getFacet(MavenCoreFacet.class); List<Plugin> pomPlugins = mavenCoreFacet.getPOM().getBuild().getPlugins(); List<MavenPlugin> plugins = new ArrayList<MavenPlugin>(); for (Plugin plugin : pomPlugins) { MavenPluginAdapter adapter = new MavenPluginAdapter(plugin); MavenPluginBuilder pluginBuilder = MavenPluginBuilder.create() .setDependency( DependencyBuilder.create() .setGroupId(plugin.getGroupId()) .setArtifactId(plugin.getArtifactId()) .setVersion(plugin.getVersion())) .setConfiguration(adapter.getConfig()); plugins.add(pluginBuilder); } return plugins; }
/* (non-Javadoc) * @see org.apache.maven.plugin.Mojo#execute() */ public void execute() throws MojoExecutionException, MojoFailureException { if (project.getFile() != null) { boolean found = false; Plugin plugin = null; // Search for this plugin in project pom for (Iterator iter = plugins.iterator(); iter.hasNext(); ) { plugin = (Plugin) iter.next(); if ("maven-cdk-plugin".equals(plugin.getArtifactId())) { String groupId; if ("3.1.0".compareTo(plugin.getVersion()) > 0) { groupId = "org.ajax4jsf.cdk"; } else { groupId = "org.richfaces.cdk"; } if (groupId.equals(plugin.getGroupId())) { found = true; break; } } } if (found) { try { createComponent(plugin); } catch (Exception e) { throw new MojoExecutionException("Error on create component", e); } } else { throw new MojoFailureException( "This project is not configured for JSF components generation"); } } else { throw new MojoFailureException("Goal 'create' must be run in existing project directory"); } }
public MojoExecution createMojoExecution(Plugin plugin, String goal, MavenProject project) throws Exception { if (plugin.getVersion() == null) { plugin.setVersion( plexusContainer .lookup(PluginVersionResolver.class) .resolve(new DefaultPluginVersionRequest(plugin, session)) .getVersion()); } MojoDescriptor mojoDescriptor = pluginManager.getMojoDescriptor( plugin, goal, project.getRemotePluginRepositories(), session.getRepositorySession()); List<PluginExecution> executions = plugin.getExecutions(); MojoExecution mojoExecution = new MojoExecution( mojoDescriptor, executions.isEmpty() ? null : executions.get(executions.size() - 1).getId(), MojoExecution.Source.CLI); plexusContainer .lookup(LifecycleExecutionPlanCalculator.class) .setupMojoExecution(session, project, mojoExecution); return mojoExecution; }
private static void assertPluginEquals(Plugin expected, Plugin actual) { assertEquals(expected, actual); assertEquals(expected.getVersion(), actual.getVersion()); assertEquals(expected.getConfiguration(), actual.getConfiguration()); }
public static void mergePluginDefinitions( Plugin child, Plugin parent, boolean handleAsInheritance) { if ((child == null) || (parent == null)) { // nothing to do. return; } if (parent.isExtensions()) { child.setExtensions(true); } if ((child.getVersion() == null) && (parent.getVersion() != null)) { child.setVersion(parent.getVersion()); } Xpp3Dom childConfiguration = (Xpp3Dom) child.getConfiguration(); Xpp3Dom parentConfiguration = (Xpp3Dom) parent.getConfiguration(); childConfiguration = Xpp3Dom.mergeXpp3Dom(childConfiguration, parentConfiguration); child.setConfiguration(childConfiguration); child.setDependencies(mergeDependencyList(child.getDependencies(), parent.getDependencies())); // from here to the end of the method is dealing with merging of the <executions/> section. String parentInherited = parent.getInherited(); boolean parentIsInherited = (parentInherited == null) || Boolean.valueOf(parentInherited).booleanValue(); List parentExecutions = parent.getExecutions(); if ((parentExecutions != null) && !parentExecutions.isEmpty()) { List mergedExecutions = new ArrayList(); Map assembledExecutions = new TreeMap(); Map childExecutions = child.getExecutionsAsMap(); for (Iterator it = parentExecutions.iterator(); it.hasNext(); ) { PluginExecution parentExecution = (PluginExecution) it.next(); String inherited = parentExecution.getInherited(); boolean parentExecInherited = parentIsInherited && ((inherited == null) || Boolean.valueOf(inherited).booleanValue()); if (!handleAsInheritance || parentExecInherited) { PluginExecution assembled = parentExecution; PluginExecution childExecution = (PluginExecution) childExecutions.get(parentExecution.getId()); if (childExecution != null) { mergePluginExecutionDefinitions(childExecution, parentExecution); assembled = childExecution; } else if (handleAsInheritance && (parentInherited == null)) { parentExecution.unsetInheritanceApplied(); } assembledExecutions.put(assembled.getId(), assembled); mergedExecutions.add(assembled); } } for (Iterator it = child.getExecutions().iterator(); it.hasNext(); ) { PluginExecution childExecution = (PluginExecution) it.next(); if (!assembledExecutions.containsKey(childExecution.getId())) { mergedExecutions.add(childExecution); } } child.setExecutions(mergedExecutions); child.flushExecutionMap(); } }
@Override public void resolveProjectDependencies( final IMavenProjectFacade facade, Set<Capability> capabilities, Set<RequiredCapability> requirements, final IProgressMonitor monitor) throws CoreException { long start = System.currentTimeMillis(); log.debug("Resolving dependencies for {}", facade.toString()); // $NON-NLS-1$ markerManager.deleteMarkers(facade.getPom(), IMavenConstants.MARKER_DEPENDENCY_ID); ProjectBuildingRequest configuration = getMaven().getExecutionContext().newProjectBuildingRequest(); configuration.setProject(facade.getMavenProject()); // TODO do we need this? configuration.setResolveDependencies(true); MavenExecutionResult mavenResult = getMaven().readMavenProject(facade.getPomFile(), configuration); markerManager.addMarkers(facade.getPom(), IMavenConstants.MARKER_DEPENDENCY_ID, mavenResult); if (!facade.getResolverConfiguration().shouldResolveWorkspaceProjects()) { return; } MavenProject mavenProject = facade.getMavenProject(); // dependencies // resolved dependencies for (Artifact artifact : mavenProject.getArtifacts()) { requirements.add( MavenRequiredCapability.createMavenArtifact( new ArtifactKey(artifact), artifact.getScope(), artifact.isOptional())); } // extension plugins (affect packaging type calculation) for (Plugin plugin : mavenProject.getBuildPlugins()) { if (plugin.isExtensions()) { ArtifactKey artifactKey = new ArtifactKey(plugin.getGroupId(), plugin.getArtifactId(), plugin.getVersion(), null); requirements.add( MavenRequiredCapability.createMavenArtifact( artifactKey, "plugin", false)); // $NON-NLS-1$ } } // missing dependencies DependencyResolutionResult resolutionResult = mavenResult.getDependencyResolutionResult(); if (resolutionResult != null && resolutionResult.getUnresolvedDependencies() != null) { for (Dependency dependency : resolutionResult.getUnresolvedDependencies()) { org.eclipse.aether.artifact.Artifact artifact = dependency.getArtifact(); ArtifactKey dependencyKey = new ArtifactKey( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), null); requirements.add( MavenRequiredCapability.createMavenArtifact( dependencyKey, dependency.getScope(), dependency.isOptional())); } } log.debug( "Resolved dependencies for {} in {} ms", facade.toString(), System.currentTimeMillis() - start); // $NON-NLS-1$ }