List of usage examples for org.apache.maven.project MavenProject getVersion
public String getVersion()
From source file:org.kuali.maven.plugins.externals.MojoHelper.java
License:Educational Community License
public GAV getGav(MavenProject project) { GAV gav = new GAV(); gav.setGroupId(project.getGroupId()); gav.setArtifactId(project.getArtifactId()); gav.setVersion(project.getVersion()); return gav;//from w w w . ja v a 2s . c om }
From source file:org.mobicents.maven.plugin.eclipse.ClasspathWriter.java
License:Open Source License
/** * Writes the .classpath file for eclipse. * /*from w w w.j a v a 2 s . c o m*/ * @param projects * the list of projects from which the .classpath will get its * dependencies. * @param repositoryVariableName * the name of the maven repository variable. * @param artifactFactory * the factory for constructing artifacts. * @param artifactResolver * the artifact resolver. * @param localRepository * the local repository instance. * @param artifactMetadataSource * @param classpathArtifactTypes * the artifacts types that are allowed in the classpath file. * @param remoteRepositories * the list of remote repository instances. * @param resolveTransitiveDependencies * whether or not dependencies shall be transitively resolved. * @param merge * anything extra (not auto-generated), that should be "merged" * into the generated .classpath * @param classpathExcludes * @param includeTestsDirectory * @param includeResourcesDirectory * @throws Exception */ public void write(final List projects, final String repositoryVariableName, final ArtifactFactory artifactFactory, final ArtifactResolver artifactResolver, final ArtifactRepository localRepository, final ArtifactMetadataSource artifactMetadataSource, final Set classpathArtifactTypes, final List remoteRepositories, final boolean resolveTransitiveDependencies, final String merge, Set classpathExcludes, boolean includeResourcesDirectory) throws Exception { final String rootDirectory = PathNormalizer.normalizePath(this.project.getBasedir().toString()); final File classpathFile = new File(rootDirectory, ".classpath"); final FileWriter fileWriter = new FileWriter(classpathFile); final XMLWriter writer = new PrettyPrintXMLWriter(fileWriter, "UTF-8", null); writer.startElement("classpath"); final Set projectArtifactIds = new LinkedHashSet(); for (final Iterator iterator = projects.iterator(); iterator.hasNext();) { final MavenProject project = (MavenProject) iterator.next(); final Artifact projectArtifact = artifactFactory.createArtifact(project.getGroupId(), project.getArtifactId(), project.getVersion(), null, project.getPackaging()); projectArtifactIds.add(projectArtifact.getId()); } // - collect the source roots for the root project (if they are any) Set<String> sourceRoots = collectSourceRoots(this.project, rootDirectory, writer, includeResourcesDirectory); final Set allArtifacts = new LinkedHashSet(this.project.createArtifacts(artifactFactory, null, null)); for (final Iterator iterator = projects.iterator(); iterator.hasNext();) { final MavenProject project = (MavenProject) iterator.next(); sourceRoots.addAll(collectSourceRoots(project, rootDirectory, writer, includeResourcesDirectory)); final Set artifacts = project.createArtifacts(artifactFactory, null, null); // - get the direct dependencies for (final Iterator artifactIterator = artifacts.iterator(); artifactIterator.hasNext();) { final Artifact artifact = (Artifact) artifactIterator.next(); // - don't attempt to resolve the artifact if its part of the // project (we // infer this if it has the same id has one of the projects or // is in // the same groupId). if (!projectArtifactIds.contains(artifact.getId()) && !project.getGroupId().equals(artifact.getGroupId())) { artifactResolver.resolve(artifact, project.getRemoteArtifactRepositories(), localRepository); allArtifacts.add(artifact); } else { allArtifacts.add(artifact); } } } // we have all source roots now, sort and write for (String sourceRoot : sourceRoots) { logger.info("Adding src path " + sourceRoot); this.writeClasspathEntry(writer, "src", sourceRoot); } // - remove the project artifacts for (final Iterator iterator = projects.iterator(); iterator.hasNext();) { final MavenProject project = (MavenProject) iterator.next(); final Artifact projectArtifact = project.getArtifact(); if (projectArtifact != null) { for (final Iterator artifactIterator = allArtifacts.iterator(); artifactIterator.hasNext();) { final Artifact artifact = (Artifact) artifactIterator.next(); final String projectId = projectArtifact.getArtifactId(); final String projectGroupId = projectArtifact.getGroupId(); final String artifactId = artifact.getArtifactId(); final String groupId = artifact.getGroupId(); if (artifactId.equals(projectId) && groupId.equals(projectGroupId)) { artifactIterator.remove(); } } } } // - now we resolve transitively, if we have the flag on if (resolveTransitiveDependencies) { final Artifact rootProjectArtifact = artifactFactory.createArtifact(this.project.getGroupId(), this.project.getArtifactId(), this.project.getVersion(), null, this.project.getPackaging()); final OrArtifactFilter filter = new OrArtifactFilter(); filter.add(new ScopeArtifactFilter(Artifact.SCOPE_COMPILE)); filter.add(new ScopeArtifactFilter(Artifact.SCOPE_PROVIDED)); filter.add(new ScopeArtifactFilter(Artifact.SCOPE_TEST)); final ArtifactResolutionResult result = artifactResolver.resolveTransitively(allArtifacts, rootProjectArtifact, localRepository, remoteRepositories, artifactMetadataSource, filter); allArtifacts.clear(); allArtifacts.addAll(result.getArtifacts()); } // remove excluded ones for (Iterator i = allArtifacts.iterator(); i.hasNext();) { Artifact artifact = (Artifact) i.next(); if (classpathExcludes != null) { if (classpathExcludes.contains(artifact.getGroupId())) { logger.info("Excluding " + artifact + " from .classpath, groupId is excluded"); i.remove(); } else if (classpathExcludes.contains(artifact.getGroupId() + ":" + artifact.getArtifactId())) { logger.info("Excluding " + artifact + " from .classpath, groupId:artifactId is excluded"); i.remove(); } else if (classpathExcludes.contains( artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion())) { logger.info( "Excluding " + artifact + " from .classpath, groupId:artifactId:version is excluded"); i.remove(); } } } final List allArtifactPaths = new ArrayList(allArtifacts); for (final ListIterator iterator = allArtifactPaths.listIterator(); iterator.hasNext();) { final Artifact artifact = (Artifact) iterator.next(); if (classpathArtifactTypes.contains(artifact.getType())) { File artifactFile = artifact.getFile(); if (artifactFile == null) { artifactResolver.resolve(artifact, project.getRemoteArtifactRepositories(), localRepository); artifactFile = artifact.getFile(); } if (artifactFile != null) { final String path = StringUtils.replace(PathNormalizer.normalizePath(artifactFile.toString()), PathNormalizer.normalizePath(localRepository.getBasedir()), repositoryVariableName); iterator.set(path); } else { iterator.remove(); } } else { iterator.remove(); } } // - sort the paths Collections.sort(allArtifactPaths); for (final Iterator iterator = allArtifactPaths.iterator(); iterator.hasNext();) { String path = (String) iterator.next(); if (path.startsWith(repositoryVariableName)) { this.writeClasspathEntry(writer, "var", path); } else { if (path.startsWith(rootDirectory)) { path = StringUtils.replace(path, rootDirectory + '/', ""); } this.writeClasspathEntry(writer, "lib", path); } } this.writeClasspathEntry(writer, "con", "org.eclipse.jdt.launching.JRE_CONTAINER"); String outputPath = StringUtils.replace( PathNormalizer.normalizePath(this.project.getBuild().getOutputDirectory()), rootDirectory, ""); if (outputPath.startsWith("/")) { outputPath = outputPath.substring(1, outputPath.length()); } this.writeClasspathEntry(writer, "output", outputPath); if (StringUtils.isNotBlank(merge)) { writer.writeMarkup(merge); } writer.endElement(); logger.info("Classpath file written --> '" + classpathFile + "'"); IOUtil.close(fileWriter); }
From source file:org.neo4j.build.plugins.ease.EaseHelper.java
License:Apache License
static void writeAndAttachArtifactList(StringBuilder builder, MavenProject project, MavenProjectHelper projectHelper, Log log) throws MojoExecutionException { String buildDir = project.getBuild().getDirectory(); String destFile = buildDir + File.separator + project.getArtifactId() + "-" + project.getVersion() + "-artifacts.txt"; try {/*from w w w.j a va2 s . c o m*/ if (FileUtils.fileExists(destFile)) { FileUtils.fileDelete(destFile); } if (!FileUtils.fileExists(buildDir)) { FileUtils.mkdir(buildDir); } FileUtils.fileWrite(destFile, "UTF-8", builder.toString()); } catch (IOException ioe) { throw new MojoExecutionException("Could not write artifact list.", ioe); } projectHelper.attachArtifact(project, "txt", "artifacts", FileUtils.getFile(destFile)); log.info("Successfully attached artifact list to the project."); }
From source file:org.opennms.maven.plugins.tgz.AbstractAssemblyMojo.java
License:Apache License
private String getOutputDirectory(String output, MavenProject project, boolean includeBaseDirectory) { String value = output;//w w w .ja v a2 s.c o m if (value == null) { value = ""; } if (!value.endsWith("/") && !value.endsWith("\\")) { // TODO: shouldn't archiver do this? value += '/'; } if (includeBaseDirectory) { if (value.startsWith("/")) { value = finalName + value; } else { value = finalName + "/" + value; } } else { if (value.startsWith("/")) { value = value.substring(1); } } if (project != null) { value = StringUtils.replace(value, "${groupId}", project.getGroupId()); value = StringUtils.replace(value, "${artifactId}", project.getArtifactId()); value = StringUtils.replace(value, "${version}", project.getVersion()); Build build = project.getBuild(); value = StringUtils.replace(value, "${build.finalName}", build.getFinalName()); value = StringUtils.replace(value, "${finalName}", build.getFinalName()); } return value; }
From source file:org.ops4j.pax.construct.clone.CloneMojo.java
License:Apache License
/** * Analyze major project and build the right pax-create-project call * //from w w w. j a va 2 s . c om * @param script build script * @param project major Maven project */ private void handleMajorProject(PaxScript script, MavenProject project) { if (unify && !project.isExecutionRoot()) { // exclude the local poms settings from the unified project m_handledDirs.add(new File(project.getBasedir(), "poms")); return; } PaxCommandBuilder command = script.call(PaxScript.CREATE_PROJECT); command.option('g', project.getGroupId()); command.option('a', project.getArtifactId()); command.option('v', project.getVersion()); setTargetDirectory(command, project.getBasedir().getParentFile()); registerProject(project); m_majorProjectMap.put(project, command); }
From source file:org.ops4j.pax.construct.clone.CloneMojo.java
License:Apache License
/** * Analyze bundle project and determine if any pax-create-bundle or pax-wrap-jar calls are needed * /*from w ww .ja v a2 s.c o m*/ * @param script build script * @param project Maven bundle project * @throws MojoExecutionException */ private void handleBundleProject(PaxScript script, MavenProject project) throws MojoExecutionException { PaxCommandBuilder command; String bundleName; String namespace = findBundleNamespace(project); if (null != namespace) { bundleName = project.getArtifactId(); command = script.call(PaxScript.CREATE_BUNDLE); command.option('p', namespace); if (!bundleName.equals(namespace)) { command.option('n', bundleName); } command.option('v', project.getVersion()); command.maven().flag("noDeps"); } else { Dependency wrappee = findWrappee(project); if (wrappee != null) { command = script.call(PaxScript.WRAP_JAR); command.option('g', wrappee.getGroupId()); command.option('a', wrappee.getArtifactId()); command.option('v', wrappee.getVersion()); if (repair) { // this is expected to be the generated bundle name bundleName = PomUtils.getCompoundId(wrappee.getGroupId(), wrappee.getArtifactId()); // detect if we need to add the version back later on... if (project.getArtifactId().endsWith(wrappee.getVersion())) { command.maven().flag("addVersion"); } } else { bundleName = project.getArtifactId(); // need to retain the old name and version settings command.maven().option("bundleName", bundleName); command.maven().option("bundleVersion", project.getVersion()); } } else { getLog().warn("Unable to clone bundle project " + project.getId()); return; } } Pom customizedPom = null; if (repair) { // fix references to local bundles by re-importing customizedPom = repairBundleImports(script, project); } else { // need to keep old groupId intact (name is already retained) command.maven().option("bundleGroupId", project.getGroupId()); } addFragmentToCommand(command, createBundleArchetype(project, namespace, customizedPom)); setTargetDirectory(command, project.getBasedir().getParentFile()); registerProject(project); registerBundleName(project, bundleName); }
From source file:org.ops4j.pax.construct.clone.CloneMojo.java
License:Apache License
/** * Create a new archetype for a bundle project, with potentially customized POM and Bnd settings * /*from www. ja va 2 s . co m*/ * @param project Maven project * @param namespace Java namespace, may be null * @param customizedPom customized Maven project model, may be null * @return clause identifying the archetype fragment * @throws MojoExecutionException */ private String createBundleArchetype(MavenProject project, String namespace, Pom customizedPom) throws MojoExecutionException { File baseDir = project.getBasedir(); getLog().info("Cloning bundle project " + project.getArtifactId()); ArchetypeFragment fragment = new ArchetypeFragment(getFragmentDir(), namespace, false); fragment.addPom(baseDir, customizedPom); if (null != namespace) { fragment.addSources(baseDir, project.getBuild().getSourceDirectory(), false); fragment.addSources(baseDir, project.getBuild().getTestSourceDirectory(), true); } for (Iterator i = project.getTestResources().iterator(); i.hasNext();) { Resource r = (Resource) i.next(); fragment.addResources(baseDir, r.getDirectory(), r.getIncludes(), r.getExcludes(), true); } List excludes = new ArrayList(); excludes.addAll(fragment.getIncludedFiles()); excludes.add("target/"); excludes.add("runner/"); excludes.add("pom.xml"); // consider everything else in the bundle directory to be a resource fragment.addResources(baseDir, baseDir.getPath(), null, excludes, false); // archetype must use different id String groupId = project.getGroupId(); String artifactId = project.getArtifactId() + "-archetype"; String version = project.getVersion(); // archive customized bundle sources, POM and Bnd instructions String fragmentId = groupId + ':' + artifactId + ':' + version; fragment.createArchive(fragmentId.replace(':', '_'), newJarArchiver()); return fragmentId; }
From source file:org.ops4j.pax.construct.clone.CloneMojo.java
License:Apache License
/** * Archive all the selected resources under a single Maven archetype * /*from w ww. j a v a 2 s . co m*/ * @param project containing Maven project * @return clause identifying the archetype fragment * @throws MojoExecutionException */ private String createProjectArchetype(MavenProject project) throws MojoExecutionException { File baseDir = project.getBasedir(); getLog().info("Cloning primary project " + project.getArtifactId()); ArchetypeFragment fragment = new ArchetypeFragment(getFragmentDir(), null, unify); fragment.addPom(baseDir, null); List excludes = new ArrayList(); excludes.addAll(getExcludedPaths(project)); excludes.add("**/target/"); excludes.add("runner/"); excludes.add("pom.xml"); // consider everything else that's not been handled to be a resource fragment.addResources(baseDir, baseDir.getPath(), null, excludes, false); // archetype must use different id String groupId = project.getGroupId(); String artifactId = project.getArtifactId() + "-archetype"; String version = project.getVersion(); // archive all the customized non-bundle POMs and projects String fragmentId = groupId + ':' + artifactId + ':' + version; fragment.createArchive(fragmentId.replace(':', '_'), newJarArchiver()); return fragmentId; }
From source file:org.ops4j.pax.construct.lifecycle.EclipseOSGiMojo.java
License:Apache License
/** * Provide better naming for Pax-Construct generated OSGi bundles * //from w w w .j a va 2s .c om * @param project current Maven project * @param addVersion when true, add the project version to the name * @return an Eclipse friendly name for the bundle */ private static String getEclipseProjectName(MavenProject project, boolean addVersion) { String projectName = project.getProperties().getProperty("bundle.symbolicName"); if (null == projectName) { // fall back to standard "groupId.artifactId" but try to eliminate duplicate segments projectName = PomUtils.getCompoundId(project.getGroupId(), project.getArtifactId()); } if (addVersion) { // check for wrapper version, which reflects the version of the wrapped contents String projectVersion = project.getProperties().getProperty("wrapped.version"); if (null == projectVersion) { projectVersion = project.getVersion(); } return projectName + " [" + projectVersion + ']'; } return projectName; }
From source file:org.ops4j.pax.construct.lifecycle.ProvisionMojo.java
License:Apache License
/** * Install deployment POM in the local Maven repository * //from w ww .j a v a 2 s .c o m * @param project deployment project * @throws MojoExecutionException */ private void installDeploymentPom(MavenProject project) throws MojoExecutionException { String groupId = project.getGroupId(); String artifactId = project.getArtifactId(); String version = project.getVersion(); Artifact pomArtifact = m_factory.createProjectArtifact(groupId, artifactId, version); try { m_installer.install(project.getFile(), pomArtifact, m_localRepo); } catch (ArtifactInstallationException e) { throw new MojoExecutionException("Unable to install deployment POM " + pomArtifact); } }