List of usage examples for org.apache.maven.project MavenProject getArtifact
public Artifact getArtifact()
From source file:org.heneveld.maven.license_audit.util.Coords.java
public static Coords of(MavenProject x) { // prefer artifact coords as that will be canonical, but it might not be available, e.g. for root project if (x.getArtifact() != null) return of(x.getArtifact()); return new Coords(x.getGroupId(), x.getArtifactId(), x.getVersion(), x.getVersion(), "", ""); }
From source file:org.hoydaa.maven.plugins.ParentCheckerMojo.java
License:Apache License
public void execute() throws MojoExecutionException { MavenProject tProject = project; while (tProject != null) { Artifact parentArtifact = tProject.getParentArtifact(); if (null == parentArtifact || checkArtifacts == null || !hasValidParent(tProject)) { getLog().info("This parent '" + parentArtifact + "' is not in the list of artifacts '" + checkArtifacts + "' to be checked, skipping..."); } else {/*w w w . j a v a2 s . c o m*/ try { // get newer versions of the parent, if there is one. ArtifactVersion currentVersion = tProject.getParentArtifact().getSelectedVersion(); List<ArtifactVersion> availableVersions = artifactMetadataSource.retrieveAvailableVersions( artifactFactory.createParentArtifact(parentArtifact.getGroupId(), parentArtifact.getArtifactId(), parentArtifact.getVersion()), localRepository, remoteArtifactRepositories); List<ArtifactVersion> newVersions = getNewerVersions(currentVersion, availableVersions); // if there is newer versions available if (newVersions.size() > 0) { boolean forcedUpdateExists = false; getLog().warn("New versions available for your parent POM " + parentArtifact.toString() + " of project '" + tProject.getArtifact().toString() + "'!"); for (ArtifactVersion version : newVersions) { boolean forced = isForced(version, tProject); forcedUpdateExists = forcedUpdateExists || forced; getLog().warn(version.toString() + " (" + (forced ? "FORCED" : "not forced") + ")"); } if (forceUpgrade) { throw new MojoExecutionException(getWarningText(newVersions, tProject) + " You have to upgrade your parent POM to the latest version!"); } else if (forcedUpdateExists) { throw new MojoExecutionException(getWarningText(newVersions, tProject) + " You have to upgrade your parent POM to the latest forced update at least!"); } else { getLog().warn(getWarningText(newVersions, tProject)); } } else { getLog().info("Your parent POM's are all up-to-date, good to do dude."); } } catch (ArtifactMetadataRetrievalException e) { e.printStackTrace(); } catch (OverConstrainedVersionException e) { e.printStackTrace(); } catch (ProjectBuildingException e) { e.printStackTrace(); } } Artifact temp = tProject.getParentArtifact(); tProject = tProject.getParent(); if (null != tProject) tProject.setArtifact(temp); } }
From source file:org.hoydaa.maven.plugins.ParentCheckerMojo.java
License:Apache License
private String getWarningText(List<ArtifactVersion> newVersions, MavenProject project) { return "Parent POM '" + project.getParentArtifact().toString() + "' of project '" + project.getArtifact().toString() + "' is " + newVersions.size() + " versions behind, you have to upgrade it to " + newVersions.get(newVersions.size() - 1) + "!"; }
From source file:org.hsc.novelSpider.bundleplugin.BundlePlugin.java
License:Apache License
protected void execute(MavenProject currentProject, Map<String, String> originalInstructions, Properties properties, Jar[] classpath) throws MojoExecutionException { getLog().warn("2 execute "); try {//from w w w.java2s . c o m File jarFile = new File(getBuildDirectory(), getBundleName(currentProject)); getLog().warn(" jarFile:" + jarFile.getAbsolutePath()); Builder builder = buildOSGiBundle(currentProject, originalInstructions, properties, classpath); boolean hasErrors = reportErrors("Bundle " + currentProject.getArtifact(), builder); if (hasErrors) { String failok = builder.getProperty("-failok"); if (null == failok || "false".equalsIgnoreCase(failok)) { jarFile.delete(); throw new MojoFailureException("Error(s) found in bundle configuration"); } } // attach bundle to maven project jarFile.getParentFile().mkdirs(); builder.getJar().write(jarFile); Artifact mainArtifact = currentProject.getArtifact(); if ("bundle".equals(mainArtifact.getType())) { // workaround for MNG-1682: force maven to install artifact using the "jar" handler mainArtifact.setArtifactHandler(m_artifactHandlerManager.getArtifactHandler("jar")); } if (null == classifier || classifier.trim().length() == 0) { mainArtifact.setFile(jarFile); } else { m_projectHelper.attachArtifact(currentProject, jarFile, classifier); } if (unpackBundle) { unpackBundle(jarFile); } if (manifestLocation != null) { File outputFile = new File(manifestLocation, "MANIFEST.MF"); try { Manifest manifest = builder.getJar().getManifest(); ManifestPlugin.writeManifest(manifest, outputFile); } catch (IOException e) { getLog().error("Error trying to write Manifest to file " + outputFile, e); } } // cleanup... builder.close(); } catch (MojoFailureException e) { getLog().error(e.getLocalizedMessage(), e); throw new MojoExecutionException("Error(s) found in bundle configuration", e); } catch (Exception e) { getLog().error("An internal error occurred", e); throw new MojoExecutionException("OSGI", e); } }
From source file:org.hsc.novelSpider.bundleplugin.BundlePlugin.java
License:Apache License
protected Jar[] getClasspath(MavenProject currentProject) throws IOException, MojoExecutionException { List<Jar> list = new ArrayList<Jar>(); if (getOutputDirectory() != null && getOutputDirectory().exists()) { list.add(new Jar(".", getOutputDirectory())); }/*from w w w.java 2 s. c o m*/ final Collection<Artifact> artifacts = getSelectedDependencies(currentProject.getArtifacts()); for (Iterator<Artifact> it = artifacts.iterator(); it.hasNext();) { Artifact artifact = (Artifact) it.next(); if (artifact.getArtifactHandler().isAddedToClasspath()) { if (!Artifact.SCOPE_TEST.equals(artifact.getScope())) { File file = getFile(artifact); if (file == null) { getLog().warn("File is not available for artifact " + artifact + " in project " + currentProject.getArtifact()); continue; } Jar jar = new Jar(artifact.getArtifactId(), file); list.add(jar); } } } Jar[] cp = new Jar[list.size()]; list.toArray(cp); return cp; }
From source file:org.hsc.novelSpider.bundleplugin.BundlePlugin.java
License:Apache License
/** * TODO this should return getMaven2Osgi().getBundleFileName( project.getArtifact() ) *//*from ww w . j ava 2 s . com*/ protected String getBundleName(MavenProject currentProject) { String extension; try { extension = currentProject.getArtifact().getArtifactHandler().getExtension(); } catch (Throwable e) { extension = currentProject.getArtifact().getType(); } if (StringUtils.isEmpty(extension) || "bundle".equals(extension) || "pom".equals(extension)) { extension = "jar"; // just in case maven gets confused } String finalName = currentProject.getBuild().getFinalName(); if (null != classifier && classifier.trim().length() > 0) { return finalName + '-' + classifier + '.' + extension; } return finalName + '.' + extension; }
From source file:org.hsc.novelSpider.bundleplugin.BundlePlugin.java
License:Apache License
protected Properties getDefaultProperties(MavenProject currentProject) { Properties properties = new Properties(); String bsn;/*from w ww .j ava 2s. com*/ try { bsn = getMaven2OsgiConverter().getBundleSymbolicName(currentProject.getArtifact()); } catch (Exception e) { bsn = currentProject.getGroupId() + "." + currentProject.getArtifactId(); } // Setup defaults properties.put(MAVEN_SYMBOLICNAME, bsn); properties.put(Analyzer.BUNDLE_SYMBOLICNAME, bsn); properties.put(Analyzer.IMPORT_PACKAGE, "*"); properties.put(Analyzer.BUNDLE_VERSION, getMaven2OsgiConverter().getVersion(currentProject.getVersion())); // remove the extraneous Include-Resource and Private-Package entries from generated manifest properties.put(Constants.REMOVEHEADERS, Analyzer.INCLUDE_RESOURCE + ',' + Analyzer.PRIVATE_PACKAGE); header(properties, Analyzer.BUNDLE_DESCRIPTION, currentProject.getDescription()); StringBuffer licenseText = printLicenses(currentProject.getLicenses()); if (licenseText != null) { header(properties, Analyzer.BUNDLE_LICENSE, licenseText); } header(properties, Analyzer.BUNDLE_NAME, currentProject.getName()); if (currentProject.getOrganization() != null) { if (currentProject.getOrganization().getName() != null) { String organizationName = currentProject.getOrganization().getName(); header(properties, Analyzer.BUNDLE_VENDOR, organizationName); properties.put("project.organization.name", organizationName); properties.put("pom.organization.name", organizationName); } if (currentProject.getOrganization().getUrl() != null) { String organizationUrl = currentProject.getOrganization().getUrl(); header(properties, Analyzer.BUNDLE_DOCURL, organizationUrl); properties.put("project.organization.url", organizationUrl); properties.put("pom.organization.url", organizationUrl); } } properties.putAll(currentProject.getProperties()); properties.putAll(currentProject.getModel().getProperties()); if (m_mavenSession != null) { try { // don't pass upper-case session settings to bnd as they end up in the manifest Properties sessionProperties = m_mavenSession.getExecutionProperties(); for (Enumeration e = sessionProperties.propertyNames(); e.hasMoreElements();) { String key = (String) e.nextElement(); if (key.length() > 0 && !Character.isUpperCase(key.charAt(0))) { properties.put(key, sessionProperties.getProperty(key)); } } } catch (Exception e) { getLog().warn("Problem with Maven session properties: " + e.getLocalizedMessage()); } } properties.putAll(getProperties(currentProject.getModel(), "project.build.")); properties.putAll(getProperties(currentProject.getModel(), "pom.")); properties.putAll(getProperties(currentProject.getModel(), "project.")); properties.put("project.baseDir", getBase(currentProject)); properties.put("project.build.directory", getBuildDirectory()); properties.put("project.build.outputdirectory", getOutputDirectory()); properties.put("classifier", classifier == null ? "" : classifier); getLog().info("BlueprintPlugin"); // Add default plugins header(properties, Analyzer.PLUGIN, SpringXMLType.class.getName()); //header( properties, Analyzer.PLUGIN, BlueprintPlugin.class.getName() + "," + SpringXMLType.class.getName() ); return properties; }
From source file:org.hsc.novelSpider.bundleplugin.ManifestPlugin.java
License:Apache License
public Manifest getManifest(MavenProject project, Map<String, String> instructions, Properties properties, Jar[] classpath) throws IOException, MojoFailureException, MojoExecutionException, Exception { Analyzer analyzer = getAnalyzer(project, instructions, properties, classpath); boolean hasErrors = reportErrors("Manifest " + project.getArtifact(), analyzer); if (hasErrors) { String failok = analyzer.getProperty("-failok"); if (null == failok || "false".equalsIgnoreCase(failok)) { throw new MojoFailureException("Error(s) found in manifest configuration"); }// w w w . j a v a 2 s . co m } Jar jar = analyzer.getJar(); if (unpackBundle) { File outputFile = getOutputDirectory(); for (Entry<String, Resource> entry : jar.getResources().entrySet()) { File entryFile = new File(outputFile, entry.getKey()); if (!entryFile.exists() || entry.getValue().lastModified() == 0) { entryFile.getParentFile().mkdirs(); OutputStream os = new FileOutputStream(entryFile); entry.getValue().write(os); os.close(); } } } Manifest manifest = jar.getManifest(); // cleanup... analyzer.close(); return manifest; }
From source file:org.hsc.novelSpider.bundleplugin.ManifestPlugin.java
License:Apache License
protected Analyzer getAnalyzer(MavenProject project, Map<String, String> instructions, Properties properties, Jar[] classpath) throws IOException, MojoExecutionException, Exception { if (rebuildBundle && supportedProjectTypes.contains(project.getArtifact().getType())) { return buildOSGiBundle(project, instructions, properties, classpath); }/* w w w.ja va2s. co m*/ File file = project.getArtifact().getFile(); if (file == null) { file = getOutputDirectory(); } if (!file.exists()) { throw new FileNotFoundException(file.getPath()); } Builder analyzer = getOSGiBuilder(project, instructions, properties, classpath); analyzer.setJar(file); // calculateExportsFromContents when we have no explicit instructions defining // the contents of the bundle *and* we are not analyzing the output directory, // otherwise fall-back to addMavenInstructions approach boolean isOutputDirectory = file.equals(getOutputDirectory()); /*EXPORT_PACKAGE if ( analyzer.getProperty( Analyzer.EXPORT_PACKAGE ) == null && analyzer.getProperty( Analyzer.EXPORT_CONTENTS ) == null && analyzer.getProperty( Analyzer.PRIVATE_PACKAGE ) == null && !isOutputDirectory ){ String export = calculateExportsFromContents( analyzer.getJar() ); analyzer.setProperty( Analyzer.EXPORT_PACKAGE, export ); } */ addMavenInstructions(project, analyzer); // if we spot Embed-Dependency and the bundle is "target/classes", assume we need to rebuild if (analyzer.getProperty(DependencyEmbedder.EMBED_DEPENDENCY) != null && isOutputDirectory) { analyzer.build(); } else { analyzer.mergeManifest(analyzer.getJar().getManifest()); analyzer.calcManifest(); } mergeMavenManifest(project, analyzer); return analyzer; }
From source file:org.itcollege.valge.licenseaudit.LicenseAuditMojo.java
@SuppressWarnings("unchecked") public List<Dependency> loadAllDependencies(MavenProject project, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories, MavenProjectBuilder mavenProjectBuilder) throws MojoExecutionException { Set<Artifact> artifacts = project.getArtifacts(); List<Dependency> deps = Lists.newArrayList(); for (Artifact artifact : artifacts) { try {/*from w w w . j av a2 s . c o m*/ MavenProject depMavenProject = mavenProjectBuilder.buildFromRepository(artifact, remoteRepositories, localRepository, true); depMavenProject.getArtifact().setScope(artifact.getScope()); deps.add(new Dependency(artifact, depMavenProject)); } catch (ProjectBuildingException e) { throw new MojoExecutionException("Failed to load dependencies", e); } } return deps; }