List of usage examples for org.apache.maven.project MavenProject getBuild
public Build getBuild()
From source file:org.jfrog.jade.plugins.idea.IdeaModuleMojo.java
License:Apache License
public void rewriteModule() throws MojoExecutionException { MavenProject executedProject = getExecutedProject(); File moduleFile = new File(executedProject.getBasedir(), getIdeProjectName() + ".iml"); try {// w ww . j ava2s . c o m Document document = readXmlDocument(moduleFile, "module.xml"); Element module = document.getRootElement(); // TODO: how can we let the WAR/EJBs plugin hook in and provide this? // TODO: merge in ejb-module, etc. if ("war".equals(executedProject.getPackaging())) { addWebModule(module); } else if ("ejb".equals(executedProject.getPackaging())) { addEjbModule(module); } else if ("ear".equals(executedProject.getPackaging())) { addEarModule(module); } else if (isIdeaPlugin()) { addPluginModule(module); } Element component = findComponent(module, "NewModuleRootManager"); Element output = findElement(component, "output"); output.addAttribute("url", getModuleFileUrl(executedProject.getBuild().getOutputDirectory())); Element outputTest = findElement(component, "output-test"); outputTest.addAttribute("url", getModuleFileUrl(executedProject.getBuild().getTestOutputDirectory())); Element content = findElement(component, "content"); removeOldElements(content, "sourceFolder"); for (Iterator i = executedProject.getCompileSourceRoots().iterator(); i.hasNext();) { String directory = (String) i.next(); addSourceFolder(content, directory, false); } for (Iterator i = executedProject.getTestCompileSourceRoots().iterator(); i.hasNext();) { String directory = (String) i.next(); addSourceFolder(content, directory, true); } for (Iterator i = executedProject.getBuild().getResources().iterator(); i.hasNext();) { Resource resource = (Resource) i.next(); String directory = resource.getDirectory(); if (resource.getTargetPath() == null && !resource.isFiltering()) { addSourceFolder(content, directory, false); } else { getLog().info( "Not adding resource directory as it has an incompatible target path or filtering: " + directory); } } for (Iterator i = executedProject.getBuild().getTestResources().iterator(); i.hasNext();) { Resource resource = (Resource) i.next(); String directory = resource.getDirectory(); if (resource.getTargetPath() == null && !resource.isFiltering()) { addSourceFolder(content, directory, true); } else { getLog().info( "Not adding test resource directory as it has an incompatible target path or filtering: " + directory); } } removeOldElements(content, "excludeFolder"); //For excludeFolder File target = new File(executedProject.getBuild().getDirectory()); File classes = new File(executedProject.getBuild().getOutputDirectory()); File testClasses = new File(executedProject.getBuild().getTestOutputDirectory()); List sourceFolders = content.elements("sourceFolder"); List<String> filteredExcludes = new ArrayList<String>(); filteredExcludes.addAll(getExcludedDirectories(target, filteredExcludes, sourceFolders)); filteredExcludes.addAll(getExcludedDirectories(classes, filteredExcludes, sourceFolders)); filteredExcludes.addAll(getExcludedDirectories(testClasses, filteredExcludes, sourceFolders)); if (getExclude() != null) { String[] dirs = getExclude().split("[,\\s]+"); for (int i = 0; i < dirs.length; i++) { File excludedDir = new File(executedProject.getBasedir(), dirs[i]); filteredExcludes.addAll(getExcludedDirectories(excludedDir, filteredExcludes, sourceFolders)); } } // even though we just ran all the directories in the filteredExcludes List through the intelligent // getExcludedDirectories method, we never actually were guaranteed the order that they were added was // in the order required to make the most optimized exclude list. In addition, the smart logic from // that method is entirely skipped if the directory doesn't currently exist. A simple string matching // will do pretty much the same thing and make the list more concise. List<String> actuallyExcluded = new ArrayList<String>(); Collections.sort(filteredExcludes); for (Iterator i = filteredExcludes.iterator(); i.hasNext();) { String dirToExclude = i.next().toString(); String dirToExcludeTemp = dirToExclude.replace('\\', '/'); boolean addExclude = true; for (Iterator iterator = actuallyExcluded.iterator(); iterator.hasNext();) { String dir = iterator.next().toString(); String dirTemp = dir.replace('\\', '/'); if (dirToExcludeTemp.startsWith(dirTemp + "/")) { addExclude = false; break; } else if (dir.startsWith(dirToExcludeTemp + "/")) { actuallyExcluded.remove(dir); } } if (addExclude) { actuallyExcluded.add(dirToExclude); addExcludeFolder(content, dirToExclude); } } rewriteDependencies(component); writeXmlDocument(moduleFile, document); } catch (DocumentException e) { throw new MojoExecutionException("Error parsing existing IML file " + moduleFile.getAbsolutePath(), e); } catch (IOException e) { throw new MojoExecutionException("Error parsing existing IML file " + moduleFile.getAbsolutePath(), e); } }
From source file:org.jfrog.jade.plugins.idea.IdeaModuleMojo.java
License:Apache License
private void addEjbModule(Element module) { module.addAttribute("type", "J2EE_EJB_MODULE"); MavenProject executedProject = getExecutedProject(); String explodedDir = executedProject.getBuild().getDirectory() + "/" + getIdeProjectName(); Element component = findComponent(module, "EjbModuleBuildComponent"); Element setting = findSetting(component, "EXPLODED_URL"); setting.addAttribute("value", getModuleFileUrl(explodedDir)); component = findComponent(module, "EjbModuleProperties"); addDeploymentDescriptor(component, "ejb-jar.xml", "2.x", "src/main/resources/META-INF/ejb-jar.xml"); removeOldElements(component, "containerElement"); List artifacts = executedProject.getTestArtifacts(); for (Iterator i = artifacts.iterator(); i.hasNext();) { Artifact artifact = (Artifact) i.next(); Element containerElement = createElement(component, "containerElement"); if (isLinkModules() && isReactorProject(artifact.getGroupId(), artifact.getArtifactId())) { containerElement.addAttribute("type", "module"); containerElement.addAttribute("name", getNameProvider().getProjectName(artifact)); Element methodAttribute = createElement(containerElement, "attribute"); methodAttribute.addAttribute("name", "method"); methodAttribute.addAttribute("value", "6"); Element uriAttribute = createElement(containerElement, "attribute"); uriAttribute.addAttribute("name", "URI"); uriAttribute.addAttribute("value", "/WEB-INF/classes"); } else if (artifact.getFile() != null) { containerElement.addAttribute("type", "library"); containerElement.addAttribute("level", "module"); containerElement.addAttribute("name", getNameProvider().getProjectName(artifact)); Element methodAttribute = createElement(containerElement, "attribute"); methodAttribute.addAttribute("name", "method"); methodAttribute.addAttribute("value", "2"); Element uriAttribute = createElement(containerElement, "attribute"); uriAttribute.addAttribute("name", "URI"); uriAttribute.addAttribute("value", "/WEB-INF/lib/" + artifact.getFile().getName()); }//from www. j a v a 2 s. c o m } }
From source file:org.jfrog.jade.plugins.idea.IdeaModuleMojo.java
License:Apache License
/** * Adds the Web module to the (.iml) project file. * * @param module Xpp3Dom element/*from w w w . ja v a 2 s . c o m*/ */ private void addWebModule(Element module) { // TODO: this is bad - reproducing war plugin defaults, etc! // --> this is where the OGNL out of a plugin would be helpful as we could run package first and // grab stuff from the mojo MavenProject executedProject = getExecutedProject(); String warWebapp = executedProject.getBuild().getDirectory() + "/" + executedProject.getArtifactId(); String warSrc = getPluginSetting("maven-war-plugin", "warSourceDirectory", "src/main/webapp"); String webXml = warSrc + "/WEB-INF/web.xml"; module.addAttribute("type", "J2EE_WEB_MODULE"); Element component = findComponent(module, "WebModuleBuildComponent"); Element setting = findSetting(component, "EXPLODED_URL"); setting.addAttribute("value", getModuleFileUrl(warWebapp)); component = findComponent(module, "WebModuleProperties"); removeOldElements(component, "containerElement"); List artifacts = executedProject.getTestArtifacts(); for (Iterator i = artifacts.iterator(); i.hasNext();) { Artifact artifact = (Artifact) i.next(); Element containerElement = createElement(component, "containerElement"); if (isLinkModules() && isReactorProject(artifact.getGroupId(), artifact.getArtifactId())) { containerElement.addAttribute("type", "module"); containerElement.addAttribute("name", getNameProvider().getProjectName(artifact)); Element methodAttribute = createElement(containerElement, "attribute"); methodAttribute.addAttribute("name", "method"); methodAttribute.addAttribute("value", "5"); Element uriAttribute = createElement(containerElement, "attribute"); uriAttribute.addAttribute("name", "URI"); // TODO: Find a way to get this info from the war plugin uriAttribute.addAttribute("value", "/WEB-INF/lib/" + artifact.getArtifactId() + "-" + artifact.getVersion() + ".jar"); } else if (artifact.getFile() != null) { containerElement.addAttribute("type", "library"); containerElement.addAttribute("level", "module"); Element methodAttribute = createElement(containerElement, "attribute"); methodAttribute.addAttribute("name", "method"); if (Artifact.SCOPE_PROVIDED.equalsIgnoreCase(artifact.getScope())) { methodAttribute.addAttribute("value", "0");// If scope is provided, do not package. } else { methodAttribute.addAttribute("value", "1");// IntelliJ 5.0.2 is bugged and doesn't read it } Element uriAttribute = createElement(containerElement, "attribute"); uriAttribute.addAttribute("name", "URI"); uriAttribute.addAttribute("value", "/WEB-INF/lib/" + artifact.getFile().getName()); Element url = createElement(containerElement, "url"); url.setText(getLibraryUrl(artifact)); } } addDeploymentDescriptor(component, "web.xml", "2.3", webXml); Element element = findElement(component, "webroots"); removeOldElements(element, "root"); element = createElement(element, "root"); element.addAttribute("relative", "/"); element.addAttribute("url", getModuleFileUrl(warSrc)); }
From source file:org.johnstonshome.maven.pkgdep.goal.ExportGoal.java
License:Open Source License
/** * {@inheritDoc}//from w ww . j ava2 s. co m */ public void execute() throws MojoExecutionException { /* * The list of all discovered packages. */ final List<Package> packages = new LinkedList<Package>(); /* * Copy of the Maven local POM */ final MavenProject project = (MavenProject) this.getPluginContext().get("project"); /* * The default target for any discovered package. */ final Artifact thisBundle = new Artifact(project.getGroupId(), project.getArtifactId(), new VersionNumber(project.getVersion())); /* * The parser for Export-Package decalarations. */ final ImportExportParser parser = new ImportExportParser(); /* * Find any static MANIFEST.MF file(s) */ getLog().info(String.format("Processing %s files...", MANIFEST_FILE)); if (project.getBuild().getResources() != null) { for (final Object resource : project.getBuild().getResources()) { final File resourceDir = new File(((Resource) resource).getDirectory()); final File[] manifests = resourceDir.listFiles(new FilenameFilter() { public boolean accept(final File dir, final String name) { return name.equals(MANIFEST_FILE); } }); for (final File manifest : manifests) { getLog().info(manifest.getPath()); packages.addAll(parser.parseManifestExports(manifest, project.getBuild().getSourceDirectory(), thisBundle)); } } } /* * Look for the felix bundle plugin */ getLog().info(String.format("Processing %s content...", ImportExportParser.PLUGIN_ARTIFACT)); packages.addAll(parser.parsePomExports(project, thisBundle)); final Repository repository = new Repository(); for (final Package found : packages) { getLog().info(found.getName() + ":" + found.getVersions()); final Package local = repository.readPackage(found.getName()); if (local != null) { local.merge(found); repository.writePackage(local); } else { repository.writePackage(found); } } }
From source file:org.johnstonshome.maven.pkgdep.parse.ImportExportParser.java
License:Open Source License
private List<Package> parsePomDependencies(final MavenProject project, final String declaration, final Artifact defaultArtifact) { final List<Package> packages = new LinkedList<Package>(); if (project.getBuildPlugins() != null) { for (final Object plugin : project.getBuildPlugins()) { final Plugin realPlugin = (Plugin) plugin; if (realPlugin.getGroupId().equals(PLUGIN_GROUP) && realPlugin.getArtifactId().equals(PLUGIN_ARTIFACT)) { final Xpp3Dom configuration = (Xpp3Dom) realPlugin.getConfiguration(); if (configuration != null) { final Xpp3Dom instructions = configuration.getChild(PLUGIN_ELEM_INSTRUCTIONS); if (instructions != null) { final Xpp3Dom[] exports = instructions.getChildren(declaration); if (exports != null) { for (final Xpp3Dom export : exports) { packages.addAll(parseExport(export.getValue(), project.getBuild().getSourceDirectory(), defaultArtifact)); }/*from w w w .j a v a2s . co m*/ } } } } } } return packages; }
From source file:org.jooby.JoobyMojo.java
License:Apache License
@SuppressWarnings("unchecked") private static Watcher setupCompiler(final MavenProject project, final String compiler, final Consumer<String> task) throws MojoFailureException { File eclipseClasspath = new File(project.getBasedir(), ".classpath"); if ("off".equalsIgnoreCase(compiler) || eclipseClasspath.exists()) { return null; }//from ww w .j a v a 2s . com List<File> resources = resources(project.getResources()); resources.add(0, new File(project.getBuild().getSourceDirectory())); List<Path> paths = resources.stream().filter(File::exists).map(File::toPath).collect(Collectors.toList()); try { ClassLoader backloader = Thread.currentThread().getContextClassLoader(); return new Watcher((kind, path) -> { ClassLoader currentloader = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(backloader); if (path.toString().endsWith(".java")) { task.accept("compile"); } else if (path.toString().endsWith(".conf") || path.toString().endsWith(".properties")) { task.accept("compile"); } } finally { Thread.currentThread().setContextClassLoader(currentloader); } }, paths.toArray(new Path[paths.size()])); } catch (Exception ex) { throw new MojoFailureException("Can't compile source code", ex); } }
From source file:org.jszip.maven.AbstractJSZipMojo.java
License:Apache License
protected Plugin findThisPluginInProject(MavenProject project) { Plugin plugin = null;/*from ww w.jav a 2 s . c o m*/ for (Plugin b : project.getBuild().getPlugins()) { if (pluginGroupId.equals(b.getGroupId()) && pluginArtifactId.equals(b.getArtifactId())) { plugin = b.clone(); plugin.setVersion(pluginVersion); // we want to use our version break; } } if (plugin == null) { getLog().debug("Falling back to our own plugin"); plugin = new Plugin(); plugin.setGroupId(pluginGroupId); plugin.setArtifactId(pluginArtifactId); plugin.setVersion(pluginVersion); } return plugin; }
From source file:org.jszip.maven.RunMojo.java
License:Apache License
private long processResourceSourceChanges(List<MavenProject> reactorProjects, MavenProject project, long lastModified) throws ArtifactFilterException { long newLastModified = lastModified; getLog().debug("Last modified for resource sources = " + lastModified); Set<File> checked = new HashSet<File>(); for (Artifact a : getOverlayArtifacts(project, scope)) { MavenProject p = findProject(reactorProjects, a); if (p == null || p.getBuild() == null || p.getBuild().getResources() == null) { continue; }// ww w. j a v a2 s . c om boolean changed = false; boolean changedFiltered = false; for (org.apache.maven.model.Resource r : p.getBuild().getResources()) { File dir = new File(r.getDirectory()); getLog().debug("Checking last modified for " + dir); if (checked.contains(dir)) { continue; } checked.add(dir); long dirLastModified = recursiveLastModified(dir); if (lastModified < dirLastModified) { changed = true; if (r.isFiltering()) { changedFiltered = true; } } } if (changedFiltered) { getLog().info("Detected change in resources of " + ArtifactUtils.versionlessKey(a) + "..."); getLog().debug("Resource filtering is used by project, invoking Maven to handle update"); // need to let Maven handle it as its the only (although slower) safe way to do it right with filters InvocationRequest request = new DefaultInvocationRequest(); request.setPomFile(p.getFile()); request.setInteractive(false); request.setRecursive(false); request.setGoals(Collections.singletonList("process-resources")); Invoker invoker = new DefaultInvoker(); invoker.setLogger(new MavenProxyLogger()); try { invoker.execute(request); newLastModified = System.currentTimeMillis(); getLog().info("Change in resources of " + ArtifactUtils.versionlessKey(a) + " processed"); } catch (MavenInvocationException e) { getLog().info(e); } } else if (changed) { getLog().info("Detected change in resources of " + ArtifactUtils.versionlessKey(a) + "..."); getLog().debug("Resource filtering is not used by project, handling update ourselves"); // can do it fast ourselves MavenResourcesExecution mavenResourcesExecution = new MavenResourcesExecution(p.getResources(), new File(p.getBuild().getOutputDirectory()), p, p.getProperties().getProperty("project.build.sourceEncoding"), Collections.emptyList(), Collections.<String>emptyList(), session); try { mavenResourcesFiltering.filterResources(mavenResourcesExecution); newLastModified = System.currentTimeMillis(); getLog().info("Change in resources of " + ArtifactUtils.versionlessKey(a) + " processed"); } catch (MavenFilteringException e) { getLog().info(e); } } } return newLastModified; }
From source file:org.l2x6.maven.srcdeps.SrcdepsLifecycleParticipant.java
License:Apache License
private void addCleanExclude(MavenProject project, Plugin cleanPlugin) { for (PluginExecution execution : cleanPlugin.getExecutions()) { if (execution.getGoals().contains("clean")) { Object conf = execution.getConfiguration(); Xpp3Dom configuration = conf instanceof Xpp3Dom ? (Xpp3Dom) conf : new Xpp3Dom("configuration"); Xpp3Dom filesets = Optional.ofNullable(configuration).map(Dom.getOrCreateChild("filesets")).value(); Xpp3Dom fileset = new Xpp3Dom("fileset"); filesets.addChild(fileset);//from ww w . j a v a 2 s. c o m Xpp3Dom directory = new Xpp3Dom("directory"); directory.setValue(project.getBuild().getDirectory() + "/srcdeps"); fileset.addChild(directory); Xpp3Dom excludes = new Xpp3Dom("excludes"); fileset.addChild(excludes); Xpp3Dom exclude = new Xpp3Dom("exclude"); exclude.setValue("**/*"); excludes.addChild(exclude); } } }
From source file:org.lib4j.maven.mojo.Specification.java
License:Open Source License
public static Specification parse(final MavenProject project, final MojoExecution mojoExecution) throws MojoFailureException { final Plugin plugin = mojoExecution.getPlugin(); final PluginExecution pluginExecution = MojoUtil.getPluginExecution(mojoExecution); final Build build = project.getBuild(); if (build == null || build.getPlugins() == null) throw new MojoFailureException("Configuration is required"); final Xpp3Dom configuration = plugin.getConfiguration() == null ? pluginExecution == null ? null : (Xpp3Dom) pluginExecution.getConfiguration() : pluginExecution.getConfiguration() == null ? (Xpp3Dom) plugin.getConfiguration() : Xpp3Dom.mergeXpp3Dom((Xpp3Dom) plugin.getConfiguration(), (Xpp3Dom) pluginExecution.getConfiguration()); return configuration == null ? null : parse(configuration.getChild("manifest"), project); }