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.plugin.MojoExecutionException; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.util.StringUtils; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.jfrog.maven.annomojo.annotations.MojoExecute; import org.jfrog.maven.annomojo.annotations.MojoGoal; 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.StringTokenizer; /** * Creates the Project files (*.ipr) for IntelliJ Idea * * @author Edwin Punzalan */ @MojoGoal("project") @MojoExecute(phase = "generate-sources") public class IdeaProjectMojo extends AbstractProjectIdeaMojo { @MojoParameter(expression = "${groupById}", defaultValue = "true", description = "Activate IntelliJ project grouping based on groupId tree") private boolean groupIdBaseGrouping = true; /* TODO @MojoParameter(expression = "${groupPom}", defaultValue = "false", description = "Activate IntelliJ project grouping based on parent/child POM relationships") private boolean pomBaseGrouping; */ /** * Create IDEA (.ipr) project files. * * @throws org.apache.maven.plugin.MojoExecutionException * */ public void execute() throws MojoExecutionException { try { doDependencyResolution(); } catch (Exception e) { throw new MojoExecutionException("Unable to build project dependencies.", e); } rewriteProject(); } public void rewriteProject() throws MojoExecutionException { MavenProject executedProject = getExecutedProject(); String ideProjectName = getIdeProjectName(); File projectFile = new File(executedProject.getBasedir(), ideProjectName + ".ipr"); try { Document document = readXmlDocument(projectFile, "project.xml"); Element module = document.getRootElement(); // Set the jdk name if set if (getJdkName() != null) { setJdkName(module, getJdkName()); } else { String javaVersion = System.getProperty("java.version"); String defaultJdkName; if (getIdeaVersion().startsWith("4")) { defaultJdkName = "java version "" + javaVersion + """; } else { defaultJdkName = javaVersion.substring(0, 3); } getLog().info("jdkName is not set, using [java version" + javaVersion + "] as default."); setJdkName(module, defaultJdkName); } setWildcardResourcePatterns(module, getWildcardResourcePatterns()); Element component = findComponent(module, "ProjectModuleManager"); Element modules = findElement(component, "modules"); removeOldElements(modules, "module"); List<MavenProject> collectedProjects = executedProject.getCollectedProjects(); if (collectedProjects != null && !collectedProjects.isEmpty()) { Element m = createElement(modules, "module"); String projectPath = new File(executedProject.getBasedir(), getNameProvider().getProjectName(executedProject) + ".iml").getAbsolutePath(); m.addAttribute("filepath", "$PROJECT_DIR$/" + toRelative(executedProject.getBasedir(), projectPath)); // For groupId group name generation, if no name provider provided // Need to find the groupId common denominator between all projects int commonUntilIdx = 0; if (groupIdBaseGrouping && !getNameProvider().hasGroupDefinitions()) { String rootGroupId = null; for (MavenProject project : collectedProjects) { String groupId = project.getGroupId(); if (rootGroupId == null) { rootGroupId = groupId; } else { int minLength = Math.min(rootGroupId.length(), groupId.length()); int idx = 0; for (; idx < minLength; idx++) { if (rootGroupId.charAt(idx) != groupId.charAt(idx)) break; } if (idx == 0) { rootGroupId = ""; break; } rootGroupId = rootGroupId.substring(0, idx); } } if (rootGroupId != null) { commonUntilIdx = rootGroupId.length(); } } for (MavenProject p : collectedProjects) { m = createElement(modules, "module"); String modulePath = new File(p.getBasedir(), getNameProvider().getProjectName(p) + ".iml") .getAbsolutePath(); m.addAttribute("filepath", "$PROJECT_DIR$/" + toRelative(executedProject.getBasedir(), modulePath)); if (groupIdBaseGrouping) { String groupId = p.getGroupId(); if (groupId != null) { String groupName = getNameProvider().getGroupName(groupId); if (groupName == null) { // Find the most parent of all groupName = groupId.substring(commonUntilIdx); if (groupName.length() > 0 && groupName.charAt(0) == '.') { groupName = groupName.substring(1); } } if (groupName.length() > 0) { groupName = groupName.replace('.', '/'); m.addAttribute("group", groupName); } } } } // Add externalModules if (externalModules != null) { for (String externalModule : externalModules) { m = createElement(modules, "module"); m.addAttribute("filepath", externalModule); m.addAttribute("group", "External"); } } } else { Element m = createElement(modules, "module"); String modulePath = new File(executedProject.getBasedir(), getNameProvider().getProjectName(executedProject) + ".iml").getAbsolutePath(); m.addAttribute("filepath", "$PROJECT_DIR$/" + toRelative(executedProject.getBasedir(), modulePath)); } // add any PathMacros we've come across if (getMacros() != null && module.elements("UsedPathMacros").size() > 0) { Element usedPathMacros = (Element) module.elements("UsedPathMacros").get(0); removeOldElements(usedPathMacros, "macro"); for (Iterator iterator = getMacros().iterator(); iterator.hasNext();) { String macro = (String) iterator.next(); Element macroElement = createElement(usedPathMacros, "macro"); macroElement.addAttribute("name", macro); } } writeXmlDocument(projectFile, document); } catch (DocumentException e) { throw new MojoExecutionException("Error parsing existing IPR file: " + projectFile.getAbsolutePath(), e); } catch (IOException e) { throw new MojoExecutionException("Error parsing existing IPR file: " + projectFile.getAbsolutePath(), e); } } /** * Sets the name of the JDK to use. * * @param content Xpp3Dom element. * @param jdkName Name of the JDK to use. */ protected void setJdkName(Element content, String jdkName) { Element component = findComponent(content, "ProjectRootManager"); component.addAttribute("project-jdk-name", jdkName); String jdkLevel = getJdkLevel(); if (jdkLevel == null) { jdkLevel = System.getProperty("java.specification.version"); } if (jdkLevel.startsWith("1.4")) { component.addAttribute("assert-keyword", "true"); component.addAttribute("jdk-15", "false"); } else if (jdkLevel.compareTo("1.5") >= 0) { component.addAttribute("assert-keyword", "true"); component.addAttribute("jdk-15", "true"); } else { component.addAttribute("assert-keyword", "false"); } } /** * Sets the wilcard resource patterns. * * @param content Xpp3Dom element. * @param wildcardResourcePatterns The wilcard resource patterns. */ protected void setWildcardResourcePatterns(Element content, String wildcardResourcePatterns) { Element compilerConfigurationElement = findComponent(content, "CompilerConfiguration"); if (!StringUtils.isEmpty(wildcardResourcePatterns)) { removeOldElements(compilerConfigurationElement, "wildcardResourcePatterns"); Element wildcardResourcePatternsElement = createElement(compilerConfigurationElement, "wildcardResourcePatterns"); StringTokenizer wildcardResourcePatternsTokenizer = new StringTokenizer(wildcardResourcePatterns, ";"); while (wildcardResourcePatternsTokenizer.hasMoreTokens()) { String wildcardResourcePattern = wildcardResourcePatternsTokenizer.nextToken(); Element entryElement = createElement(wildcardResourcePatternsElement, "entry"); entryElement.addAttribute("name", wildcardResourcePattern); } } } }