Java tutorial
package org.jfrog.jade.plugins.idea; /* * Copyright 2005-2006 The Apache Software Foundation. * * 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. */ import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.resolver.ArtifactNotFoundException; import org.apache.maven.artifact.resolver.ArtifactResolutionException; import org.apache.maven.artifact.resolver.ArtifactResolutionResult; import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; import org.apache.maven.model.Plugin; import org.apache.maven.project.MavenProject; import org.apache.maven.project.ProjectBuildingException; import org.apache.maven.project.artifact.InvalidDependencyVersionException; import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.xml.Xpp3Dom; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import org.jfrog.jade.plugins.common.injectable.MvnInjectable; import org.jfrog.jade.plugins.ide.AbstractIdeMojo; import org.jfrog.maven.annomojo.annotations.MojoParameter; import java.io.File; import java.io.IOException; import java.util.Iterator; import java.util.List; import java.util.Map; /** * @author Edwin Punzalan * @author Frederic Simon Jade Java 5 conversion */ public abstract class AbstractIdeaMojo extends AbstractIdeMojo { @MojoParameter(expression = "${overwrite}", defaultValue = "false", description = "Whether to update the existing project files or overwrite them.") private boolean overwrite; @MojoParameter(property = "externalModules", description = "A list of external modules to nbe added to the project") protected List<String> externalModules; public boolean isOverwrite() { return overwrite; } public void setLocalRepo(ArtifactRepository localRepository) { super.setLocalRepository(localRepository); } public void updateFromMvnInjectable(MvnInjectable injectable) { super.updateFromMvnInjectable(injectable); AbstractIdeaMojo ideaMojo = ((AbstractIdeaMojo) injectable); this.overwrite = ideaMojo.overwrite; this.externalModules = ideaMojo.externalModules; } protected Document readXmlDocument(File file, String altFilename) throws DocumentException { SAXReader reader = new SAXReader(); if (file.exists() && !overwrite) { return reader.read(file); } else { File altFile; if (templatesPath != null) { altFile = new File(templatesPath, altFilename); } else { altFile = new File(executedProject.getBasedir(), "src/main/idea/" + altFilename); } if (altFile.exists() && altFile.isFile()) { return reader.read(altFile); } else { return reader.read(getClass().getResourceAsStream("/templates/default/" + altFilename)); } } } protected void writeXmlDocument(File file, Document document) throws IOException { XMLWriter writer = new IdeaXmlWriter(file); writer.write(document); writer.close(); } /** * Finds element from the module element. * * @param module Xpp3Dom element * @param name Name attribute to find * @return component Returns the Xpp3Dom element found. */ protected Element findComponent(Element module, String name) { return findElement(module, "component", name); } protected Element findElement(Element element, String elementName, String attributeName) { for (Iterator children = element.elementIterator(elementName); children.hasNext();) { Element child = (Element) children.next(); if (attributeName.equals(child.attributeValue("name"))) { return child; } } return createElement(element, elementName).addAttribute("name", attributeName); } protected Element findElement(Element component, String name) { Element element = component.element(name); if (element == null) { element = createElement(component, name); } return element; } /** * Creates an Xpp3Dom element. * * @param module Xpp3Dom element * @param name Name of the element * @return component Xpp3Dom element */ protected Element createElement(Element module, String name) { return module.addElement(name); } /** * Translate the absolutePath into its relative path. * * @param basedir The basedir of the project. * @param absolutePath The absolute path that must be translated to relative path. * @return relative Relative path of the parameter absolute path. */ protected String toRelative(File basedir, String absolutePath) { String relative; if (absolutePath.startsWith(basedir.getAbsolutePath())) { relative = absolutePath.substring(basedir.getAbsolutePath().length() + 1); } else { relative = absolutePath; } relative = StringUtils.replace(relative, "\\", "/"); return relative; } /** * Remove elements from content (Xpp3Dom). * * @param content Xpp3Dom element * @param name Name of the element to be removed */ protected void removeOldElements(Element content, String name) { for (Iterator children = content.elementIterator(); children.hasNext();) { Element child = (Element) children.next(); if (name.equals(child.getName())) { content.remove(child); } } } protected void doDependencyResolution() throws InvalidDependencyVersionException, ProjectBuildingException, InvalidVersionSpecificationException { // If the execution root project is not a parent (meaning it is last one in reactor list) // Then the mojo will inherit manually its configuration if (reactorProjects != null) { int nbProjects = reactorProjects.size(); // Get the last project it contains the specific ideaj configuration if (nbProjects > 1) { MavenProject lastproject = reactorProjects.get(nbProjects - 1); if (lastproject.isExecutionRoot()) { //noinspection unchecked List<Plugin> plugins = lastproject.getBuildPlugins(); fillPluginSettings(plugins, "jade-idea-plugin", this, null); } } } MavenProject project = getExecutedProject(); ArtifactRepository localRepo = getLocalRepository(); Map managedVersions = createManagedVersionMap(); try { ArtifactResolutionResult result = getArtifactResolver().resolveTransitively(getProjectArtifacts(), project.getArtifact(), managedVersions, localRepo, project.getRemoteArtifactRepositories(), artifactMetadataSource); project.setArtifacts(result.getArtifacts()); } catch (ArtifactNotFoundException e) { getLog().debug(e.getMessage(), e); StringBuffer msg = new StringBuffer(); msg.append("An error occurred during dependency resolution.\n\n"); msg.append(" Failed to retrieve ").append(e.getDownloadUrl()).append("\n"); msg.append("from the following repositories:"); for (Iterator repositories = e.getRemoteRepositories().iterator(); repositories.hasNext();) { ArtifactRepository repository = (ArtifactRepository) repositories.next(); msg.append("\n ").append(repository.getId()).append("(").append(repository.getUrl()).append(")"); } msg.append("\nCaused by: ").append(e.getMessage()); getLog().warn(msg); } catch (ArtifactResolutionException e) { getLog().debug(e.getMessage(), e); StringBuffer msg = new StringBuffer(); msg.append("An error occurred during dependency resolution of the following artifact:\n\n"); msg.append(" ").append(e.getGroupId()).append(":").append(e.getArtifactId()).append(e.getVersion()) .append("\n\n"); msg.append("Caused by: ").append(e.getMessage()); getLog().warn(msg); } } /* * @todo we need a more permanent feature that does this properly */ protected String getPluginSetting(String pluginArtifactId, String optionName, String defaultValue) { //noinspection unchecked List<Plugin> buildPlugins = executedProject.getBuildPlugins(); for (Plugin plugin : buildPlugins) { if (plugin.getArtifactId().equals(pluginArtifactId)) { Xpp3Dom o = (Xpp3Dom) plugin.getConfiguration(); if (o != null && o.getChild(optionName) != null) { return o.getChild(optionName).getValue(); } } } return defaultValue; } }