List of usage examples for org.apache.maven.project MavenProject getBuild
public Build getBuild()
From source file:org.codehaus.mojo.gwt.shell.ClasspathBuilder.java
License:Apache License
/** * Build classpath list using either gwtHome (if present) or using *project* dependencies. Note that this is ONLY * used for the script/cmd writers (so the scopes are not for the compiler, or war plugins, etc). This is required * so that the script writers can get the dependencies they need regardless of the Maven scopes (still want to use * the Maven scopes for everything else Maven, but for GWT-Maven we need to access deps differently - directly at * times).//from w w w .j ava 2 s .c om * * @param project The maven project the Mojo is running for * @param scope * @param runtime * @param artifacts the project artifacts (all scopes) * @return file collection for classpath * @throws DependencyResolutionRequiredException */ @SuppressWarnings("unchecked") public Collection<File> buildClasspathList(final MavenProject project, final String scope, GwtRuntime runtime, Set<Artifact> artifacts) throws MojoExecutionException { getLogger().info("establishing classpath list (scope = " + scope + ")"); Set<File> items = new LinkedHashSet<File>(); // Note : Don't call addSourceWithActiveProject as a GWT dependency MUST be a valid GWT library module : // * include java sources in the JAR as resources // * define a gwt.xml module file to declare the required inherits // addSourceWithActiveProject would make some java sources available to GWT compiler that should not be accessible in // a non-reactor build, making the build less deterministic and encouraging bad design. addSources(items, project.getCompileSourceRoots()); addResources(items, project.getResources()); items.add(new File(project.getBuild().getOutputDirectory())); // Use our own ClasspathElements fitering, as for RUNTIME we need to include PROVIDED artifacts, // that is not the default Maven policy, as RUNTIME is used here to build the GWTShell execution classpath if (scope.equals(SCOPE_TEST)) { addSources(items, project.getTestCompileSourceRoots()); addResources(items, project.getTestResources()); items.add(new File(project.getBuild().getTestOutputDirectory())); // Add all project dependencies in classpath for (Artifact artifact : artifacts) { items.add(artifact.getFile()); } } else if (scope.equals(SCOPE_COMPILE)) { // Add all project dependencies in classpath getLogger().debug("candidate artifacts : " + artifacts.size()); for (Artifact artifact : artifacts) { String artifactScope = artifact.getScope(); if (SCOPE_COMPILE.equals(artifactScope) || SCOPE_PROVIDED.equals(artifactScope) || SCOPE_SYSTEM.equals(artifactScope)) { items.add(artifact.getFile()); } } } else if (scope.equals(SCOPE_RUNTIME)) { // Add all dependencies BUT "TEST" as we need PROVIDED ones to setup the execution // GWTShell that is NOT a full JEE server for (Artifact artifact : artifacts) { getLogger().debug("candidate artifact : " + artifact); if (!artifact.getScope().equals(SCOPE_TEST) && artifact.getArtifactHandler().isAddedToClasspath()) { items.add(artifact.getFile()); } } } else { throw new IllegalArgumentException("unsupported scope " + scope); } if (runtime != null) { items.add(runtime.getGwtDevJar()); } getLogger().debug("GWT SDK execution classpath :"); for (File f : items) { getLogger().debug(" " + f.getAbsolutePath()); } return items; }
From source file:org.codehaus.mojo.macker.it.AbstractMackerPluginITCase.java
License:Apache License
/** * Execute the plugin./*from w w w . ja v a 2s . c o m*/ * @param baseDir Execute the plugin goal on a test project and verify generated files. * @param properties additional properties * @param goalList comma separated list of goals to execute * @throws Exception any exception generated during test */ protected void testProject(File baseDir, Properties properties, String goalList) throws Exception { File pom = new File(baseDir, "pom.xml"); String[] goal = goalList.split(","); List/*<String>*/ goals = new ArrayList/*<String>*/(); for (int i = 0; i < goal.length; i++) { goals.add(goal[i]); } executeMaven(pom, properties, goals, true); MavenProject project = readProject(pom); File projectOutputDir = new File(project.getBuild().getDirectory()); compareMackerOutput(baseDir, projectOutputDir); }
From source file:org.codehaus.mojo.nbm.CreateClusterMojo.java
License:Apache License
public void execute() throws MojoExecutionException, MojoFailureException { Project antProject = registerNbmAntTasks(); if (!nbmBuildDir.exists()) { nbmBuildDir.mkdirs();//w w w .ja va 2 s .c o m } if (reactorProjects != null && reactorProjects.size() > 0) { for (MavenProject proj : reactorProjects) { //TODO how to figure where the the buildDir/nbm directory is File nbmDir = new File(proj.getBasedir(), "target" + File.separator + "nbm" + File.separator + "netbeans"); if (nbmDir.exists()) { Copy copyTask = (Copy) antProject.createTask("copy"); copyTask.setTodir(nbmBuildDir); copyTask.setOverwrite(true); FileSet set = new FileSet(); set.setDir(nbmDir); set.createInclude().setName("**"); copyTask.addFileset(set); try { copyTask.execute(); } catch (BuildException ex) { getLog().error("Cannot merge modules into cluster"); throw new MojoExecutionException("Cannot merge modules into cluster", ex); } } else { if ("nbm".equals(proj.getPackaging())) { String error = "The NetBeans binary directory structure for " + proj.getId() + " is not created yet." + "\n Please execute 'mvn install nbm:cluster' to build all relevant projects in the reactor."; throw new MojoFailureException(error); } if ("bundle".equals(proj.getPackaging())) { Artifact art = proj.getArtifact(); final ExamineManifest mnf = new ExamineManifest(getLog()); File jar = new File(proj.getBuild().getDirectory(), proj.getBuild().getFinalName() + ".jar"); if (!jar.exists()) { getLog().error("Skipping " + proj.getId() + ". Cannot find the main artifact in output directory."); continue; } mnf.setJarFile(jar); mnf.checkFile(); File cluster = new File(nbmBuildDir, defaultCluster); getLog().debug("Copying " + art.getId() + " to cluster " + defaultCluster); File modules = new File(cluster, "modules"); modules.mkdirs(); File config = new File(cluster, "config"); File confModules = new File(config, "Modules"); confModules.mkdirs(); File updateTracting = new File(cluster, "update_tracking"); updateTracting.mkdirs(); final String cnb = mnf.getModule(); final String cnbDashed = cnb.replace(".", "-"); final File moduleArt = new File(modules, cnbDashed + ".jar"); //do we need the file in some canotical name pattern? final String specVer = mnf.getSpecVersion(); try { FileUtils.copyFile(jar, moduleArt); final File moduleConf = new File(confModules, cnbDashed + ".xml"); FileUtils.copyStreamToFile(new InputStreamFacade() { public InputStream getInputStream() throws IOException { return new StringInputStream(CreateClusterAppMojo.createBundleConfigFile(cnb, mnf.isBundleAutoload()), "UTF-8"); } }, moduleConf); FileUtils.copyStreamToFile(new InputStreamFacade() { public InputStream getInputStream() throws IOException { return new StringInputStream(CreateClusterAppMojo.createBundleUpdateTracking( cnb, moduleArt, moduleConf, specVer), "UTF-8"); } }, new File(updateTracting, cnbDashed + ".xml")); } catch (IOException exc) { getLog().error(exc); } } } } //in 6.1 the rebuilt modules will be cached if the timestamp is not touched. File[] files = nbmBuildDir.listFiles(); for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { File stamp = new File(files[i], ".lastModified"); if (!stamp.exists()) { try { stamp.createNewFile(); } catch (IOException ex) { ex.printStackTrace(); } } stamp.setLastModified(new Date().getTime()); } } getLog().info("Created NetBeans module cluster(s) at " + nbmBuildDir); } else { throw new MojoExecutionException("This goal only makes sense on reactor projects."); } }
From source file:org.codehaus.mojo.nbm.CreateUpdateSiteMojo.java
License:Apache License
public void execute() throws MojoExecutionException, MojoFailureException { Project antProject = registerNbmAntTasks(); File nbmBuildDirFile = new File(outputDirectory, "netbeans_site"); if (!nbmBuildDirFile.exists()) { nbmBuildDirFile.mkdirs();/*from w ww.j a v a2s . c om*/ } boolean isRepository = false; if ("auto".equals(distBase)) { distBase = null; } ArtifactRepository distRepository = getDeploymentRepository(distBase, container, getLog()); String oldDistBase = null; if (distRepository != null) { isRepository = true; } else { if (distBase != null && !distBase.contains("::")) { oldDistBase = distBase; } } if ("nbm-application".equals(project.getPackaging())) { @SuppressWarnings("unchecked") Set<Artifact> artifacts = project.getArtifacts(); for (Artifact art : artifacts) { if (!matchesIncludes(art)) { continue; } ArtifactResult res = turnJarToNbmFile(art, artifactFactory, artifactResolver, project, localRepository); if (res.hasConvertedArtifact()) { art = res.getConvertedArtifact(); } if (art.getType().equals("nbm-file")) { Copy copyTask = (Copy) antProject.createTask("copy"); copyTask.setOverwrite(true); copyTask.setFile(art.getFile()); if (!isRepository) { copyTask.setFlatten(true); copyTask.setTodir(nbmBuildDirFile); } else { String path = distRepository.pathOf(art); File f = new File(nbmBuildDirFile, path.replace('/', File.separatorChar)); copyTask.setTofile(f); } try { copyTask.execute(); } catch (BuildException ex) { throw new MojoExecutionException("Cannot merge nbm files into autoupdate site", ex); } } if (res.isOSGiBundle()) { // TODO check for bundles } } getLog().info("Created NetBeans module cluster(s) at " + nbmBuildDirFile.getAbsoluteFile()); } else if (reactorProjects != null && reactorProjects.size() > 0) { Iterator it = reactorProjects.iterator(); while (it.hasNext()) { MavenProject proj = (MavenProject) it.next(); //TODO how to figure where the the buildDir/nbm directory is File moduleDir = proj.getFile().getParentFile(); if (moduleDir != null && moduleDir.exists()) { Copy copyTask = (Copy) antProject.createTask("copy"); if (!isRepository) { FileSet fs = new FileSet(); File projOutputDirectory = new File(proj.getBuild().getDirectory()); fs.setDir(projOutputDirectory); fs.createInclude().setName("*.nbm"); copyTask.addFileset(fs); copyTask.setOverwrite(true); copyTask.setFlatten(true); copyTask.setTodir(nbmBuildDirFile); } else { File target = new File(proj.getBuild().getDirectory()); boolean has = false; File[] fls = target.listFiles(); if (fls != null) { for (File fl : fls) { if (fl.getName().endsWith(".nbm")) { copyTask.setFile(fl); has = true; break; } } } if (!has) { continue; } Artifact art = artifactFactory.createArtifact(proj.getGroupId(), proj.getArtifactId(), proj.getVersion(), null, "nbm-file"); String path = distRepository.pathOf(art); File f = new File(nbmBuildDirFile, path.replace('/', File.separatorChar)); copyTask.setTofile(f); } try { copyTask.execute(); } catch (BuildException ex) { throw new MojoExecutionException("Cannot merge nbm files into autoupdate site", ex); } } } } else { throw new MojoExecutionException( "This goal only makes sense on reactor projects or project with 'nbm-application' packaging."); } MakeUpdateDesc descTask = (MakeUpdateDesc) antProject.createTask("updatedist"); File xmlFile = new File(nbmBuildDirFile, fileName); descTask.setDesc(xmlFile); if (oldDistBase != null) { descTask.setDistBase(oldDistBase); } if (distRepository != null) { descTask.setDistBase(distRepository.getUrl()); } FileSet fs = new FileSet(); fs.setDir(nbmBuildDirFile); fs.createInclude().setName("**/*.nbm"); descTask.addFileset(fs); try { descTask.execute(); } catch (BuildException ex) { throw new MojoExecutionException("Cannot create autoupdate site xml file", ex); } getLog().info("Generated autoupdate site content at " + nbmBuildDirFile.getAbsolutePath()); try { GZipArchiver gz = new GZipArchiver(); gz.addFile(xmlFile, fileName); File gzipped = new File(nbmBuildDirFile, fileName + ".gz"); gz.setDestFile(gzipped); gz.createArchive(); if ("nbm-application".equals(project.getPackaging())) { projectHelper.attachArtifact(project, "xml.gz", "updatesite", gzipped); } } catch (Exception ex) { throw new MojoExecutionException("Cannot create gzipped version of the update site xml file.", ex); } }
From source file:org.codehaus.mojo.nbm.NetBeansManifestUpdateMojo.java
License:Apache License
/** * The current projects's dependencies, includes classes used in teh module itself * and the classpath libraries as well.// w ww . ja va 2s . co m * @param project * @param libraries * @return * @throws java.io.IOException */ private Set<String> buildProjectDependencyClasses(MavenProject project, List<Artifact> libraries) throws IOException { Set<String> dependencyClasses = new HashSet<String>(); String outputDirectory = project.getBuild().getOutputDirectory(); dependencyClasses.addAll(buildDependencyClasses(outputDirectory)); for (Artifact lib : libraries) { dependencyClasses.addAll(buildDependencyClasses(lib.getFile().getAbsolutePath())); } return dependencyClasses; }
From source file:org.codehaus.mojo.nbm.NetBeansManifestUpdateMojo.java
License:Apache License
@SuppressWarnings("unchecked") private Set<String> projectModuleOwnClasses(MavenProject project, List<Artifact> libraries) throws IOException { Set<String> projectClasses = new HashSet<String>(); DefaultClassAnalyzer analyzer = new DefaultClassAnalyzer(); String outputDirectory = project.getBuild().getOutputDirectory(); URL fl = new File(outputDirectory).toURI().toURL(); projectClasses.addAll(analyzer.analyze(fl)); for (Artifact lib : libraries) { URL url = lib.getFile().toURI().toURL(); projectClasses.addAll(analyzer.analyze(url)); }//from w ww . j a v a 2s . c om return projectClasses; }
From source file:org.codehaus.mojo.nbm.NetBeansManifestUpdateMojo.java
License:Apache License
/** * complete list of classes on project runtime classpath (excluding * jdk bit)//from ww w .ja v a 2 s .c o m * @param project * @return * @throws java.io.IOException */ @SuppressWarnings("unchecked") private Set<String> allProjectClasses(MavenProject project) throws IOException { Set<String> projectClasses = new HashSet<String>(); DefaultClassAnalyzer analyzer = new DefaultClassAnalyzer(); String outputDirectory = project.getBuild().getOutputDirectory(); URL fl = new File(outputDirectory).toURI().toURL(); projectClasses.addAll(analyzer.analyze(fl)); List<Artifact> libs = project.getRuntimeArtifacts(); for (Artifact lib : libs) { URL url = lib.getFile().toURI().toURL(); projectClasses.addAll(analyzer.analyze(url)); } return projectClasses; }
From source file:org.codehaus.mojo.runtime.builder.MavenJarRuntimeBuilder.java
License:Open Source License
public void build(Runtime runtime, MavenProject project, File runtimeFile, File outputDirectory) throws MavenRuntimeBuilderException { try {// w w w . j av a2s. co m // prepare output directories initializeOutputDirectories(runtime, outputDirectory); // add needed dependencies to the runtime object prepareRuntimeDependencies(runtime, project); prepareRuntimeClasses(); prepareRuntimeExecutionDependencies(project); // we need all the compiled classes into a jar of their own JarMaker projectJar = new JarMaker(runtimeLibraryDirectory, project.getArtifactId() + "-" + project.getVersion() + "-runtime.jar"); projectJar.addDirectory("**/**", "**/package.html,**/.svn/**", "", new File(project.getBuild().getDirectory() + File.separator + "classes")); projectJar.create(); // make a dependency reference for this newly created jar runtime.getJar().addDependency( project.getGroupId() + ":" + project.getArtifactId() + ":" + project.getVersion() + "-runtime"); // place the finalized runtime descriptor RuntimeXpp3Writer runtimeWriter = new RuntimeXpp3Writer(); FileWriter fw = new FileWriter(new File(runtimeMetaDirectory, "jar.runtime")); runtimeWriter.write(fw, runtime); fw.close(); // finally generate the end game jar JarMaker runtimeJar = new JarMaker(outputDirectory, project.getArtifactId() + "-runtime-" + project.getVersion() + ".jar"); runtimeJar.addDirectory("**/**", "**/package.html,**/.svn/**", "", runtimeOutputDirectory); runtimeJar.addManifestEntry("Main-Class", "org.codehaus.mojo.runtime.execute.RuntimeExecutor"); runtimeJar.create(); } catch (IOException ex) { throw new MavenRuntimeBuilderException("Exception while building the runtime", ex); } }
From source file:org.codehaus.mojo.unix.maven.plugin.MavenProjectWrapper.java
License:Open Source License
public static MavenProjectWrapper mavenProjectWrapper(final MavenProject project, MavenSession session) { SortedMap<String, String> properties = new TreeMap<String, String>(); // This is perhaps not ideal. Maven uses reflection to dynamically extract properties from the project // when interpolating each file. This uses a static list that doesn't contain the project.* properties, except // the new we hard-code support for. ////ww w .j a v a2 s .co m // The user can work around this like this: // <properties> // <project.build.directory>${project.build.directory}</project.build.directory> // </properties> // // If this has to change, the properties has to be a F<String, String> and interpolation tokens ("${" and "}") // has to be defined. Doable but too painful for now. properties.putAll(toMap(session.getSystemProperties())); properties.putAll(toMap(session.getUserProperties())); properties.putAll(toMap(project.getProperties())); properties.put("project.groupId", project.getGroupId()); properties.put("project.artifactId", project.getArtifactId()); properties.put("project.version", project.getVersion()); return new MavenProjectWrapper(project.getGroupId(), project.getArtifactId(), project.getVersion(), project.getArtifact(), project.getName(), project.getDescription(), project.getBasedir(), new File(project.getBuild().getDirectory()), new LocalDateTime(), project.getArtifacts(), project.getLicenses(), new ArtifactMap(project.getArtifacts()), unmodifiableSortedMap(properties)); }
From source file:org.codehaus.mojo.xml.test.AbstractXmlMojoTestCase.java
License:Apache License
protected AbstractXmlMojo newMojo(String pDir) throws Exception { File testPom = new File(new File(getBasedir(), pDir), "pom.xml"); MavenExecutionRequest executionRequest = new DefaultMavenExecutionRequest(); ProjectBuildingRequest buildingRequest = executionRequest.getProjectBuildingRequest(); ProjectBuilder projectBuilder = this.lookup(ProjectBuilder.class); MavenProject project = projectBuilder.build(testPom, buildingRequest).getProject(); // final Build build = new Build(); // build.setDirectory( "target" ); // project.setBuild(build); project.getBuild().setDirectory("target"); AbstractXmlMojo vm = (AbstractXmlMojo) lookupConfiguredMojo(project, getGoal()); setVariableValueToObject(vm, "basedir", new File(getBasedir(), pDir)); final Log log = new SilentLog(); DefaultResourceManager rm = new DefaultResourceManager(); setVariableValueToObject(rm, "logger", log); setVariableValueToObject(vm, "locator", rm); final Map<String, ResourceLoader> resourceLoaders = new HashMap<String, ResourceLoader>(); resourceLoaders.put("file", new FileResourceLoader()); resourceLoaders.put("jar", new JarResourceLoader()); resourceLoaders.put("classloader", new ThreadContextClasspathResourceLoader()); URLResourceLoader url = new URLResourceLoader(); setVariableValueToObject(url, "logger", log); resourceLoaders.put("url", url); setVariableValueToObject(rm, "resourceLoaders", resourceLoaders); // MavenProjectStub project = new MavenProjectStub() // {/*from w w w . j a v a 2s . c o m*/ // public Build getBuild() // { // return build; // } // }; // setVariableValueToObject( vm, "project", project ); return vm; }