List of usage examples for org.apache.maven.project MavenProject getArtifact
public Artifact getArtifact()
From source file:org.wso2.maven.dashboards.FileManagementUtils.java
License:Open Source License
public static void processMavenProject(File project, String artifactType, MavenProject mavenProject, File path) throws MojoExecutionException { try {// w ww . j a v a 2 s . co m String artifactName = mavenProject.getArtifactId() + SEPERATOR + mavenProject.getVersion() + FILE_TYPE_SEPERATOR + artifactType; File archive = createArchive(path, project, artifactName); if (archive != null && archive.exists()) { mavenProject.getArtifact().setFile(archive); } } catch (IOException e) { throw new MojoExecutionException("Error while creating gadget archive " + mavenProject.getArtifactId() + FILE_TYPE_SEPERATOR + artifactType, e); } }
From source file:se.jguru.nazgul.tools.codestyle.enforcer.rules.CorrectPackagingRule.java
License:Apache License
/** * Delegate method, implemented by concrete subclasses. * * @param project The active MavenProject. * @param helper The EnforcerRuleHelper instance, from which the MavenProject has been retrieved. * @throws RuleFailureException If the enforcer rule was not satisfied. *//*from w ww . ja va2 s .c o m*/ @Override @SuppressWarnings("unchecked") protected void performValidation(final MavenProject project, final EnforcerRuleHelper helper) throws RuleFailureException { // Find all java source files, and map their packages to their names. final List<String> compileSourceRoots = (List<String>) project.getCompileSourceRoots(); if (compileSourceRoots.size() == 0) { return; } final SortedMap<String, SortedSet<String>> packageName2SourceFileNameMap = new TreeMap<String, SortedSet<String>>(); for (String current : compileSourceRoots) { addPackages(new File(current), packageName2SourceFileNameMap); } // Retrieve the groupId of this project final String groupId = project.getGroupId(); if (groupId == null || groupId.equals("")) { // Don't accept empty groupIds throw new RuleFailureException("Maven groupId cannot be null or empty.", project.getArtifact()); } else { // Correct packaging everywhere? final SortedSet<String> incorrectPackages = new TreeSet<String>(); for (Map.Entry<String, SortedSet<String>> currentPackage : packageName2SourceFileNameMap.entrySet()) { final String candidate = currentPackage.getKey(); if (!candidate.startsWith(groupId)) { incorrectPackages.add(candidate); } } if (incorrectPackages.size() > 0) { final SortedMap<String, SortedSet<String>> result = new TreeMap<String, SortedSet<String>>(); for (String current : incorrectPackages) { result.put(current, packageName2SourceFileNameMap.get(current)); } throw new RuleFailureException("Incorrect packaging detected; required [" + groupId + "] but found package to file names: " + result, project.getArtifact()); } } }
From source file:se.natusoft.tools.codelicmgr.MojoUtils.java
License:Open Source License
/** * Updates third party license information from maven project information. * * @param thirdpartyLicenses The third party license configuration to update. * @param mavenProject The running maven project. * @param localRepository The artifact repository for the current build. * @param log To log to.//w w w . j a v a 2 s . c o m * @return The passed configuration. */ public static void updateThirdpartyLicenseConfigFromMavenProject(ThirdpartyLicensesConfig thirdpartyLicenses, MavenProject mavenProject, ArtifactRepository localRepository, Log log) { Set<Artifact> dependencies = mavenProject.getDependencyArtifacts(); if (dependencies != null) { for (Artifact depArtifact : dependencies) { if (!depArtifact.getType().equals("pom") && // Avoid test scope dependencies !depArtifact.getScope().equals(Artifact.SCOPE_TEST) && // Avoid internal dependencies. !depArtifact.getGroupId().equals(mavenProject.getArtifact().getGroupId())) { try { PomExtractor depPom = new PomExtractor(localRepository, depArtifact); ProductConfig newProdConfig = new ProductConfig(); newProdConfig.setName(depArtifact.getArtifactId()); newProdConfig.setVersion(depArtifact.getVersion()); newProdConfig.setWeb(depPom.getProductUrl()); if (depPom.getLicenseName() != null) { String licName = getLicenseName(depPom.getLicenseName()); String licVer = getLicenseVersion(depPom.getLicenseName()); // Lets try the name we extracted first ThirdpartyLicenseConfig tplConfig = lookupThirdpartyLicense(thirdpartyLicenses, licName); // That failed, try to construct an acronym of the name instead. if (tplConfig == null) { String altLicName = getLicenseNameAcronym(depPom.getLicenseName()); tplConfig = lookupThirdpartyLicense(thirdpartyLicenses, altLicName); if (tplConfig != null) { licName = altLicName; } else {// Not among the already known third party licenses, look in license library next. try { LibraryLicense libLic = LicenseLibrary.getLicense(altLicName, licVer, ""); if (libLic != null && !libLic.isDownloadable()) { String type = libLic.getType(); if (type != null) { licName = type; } } } catch (CodeLicenseException cle) { } } } // That also failed, now we try to remove all spaces from the name. if (tplConfig == null) { String altLicName = getLicenseName(depPom.getLicenseName()).replace(" ", ""); tplConfig = lookupThirdpartyLicense(thirdpartyLicenses, altLicName); if (tplConfig != null) { licName = altLicName; } else {// Not among the already known third party licenses, look in license library next. try { LibraryLicense libLic = LicenseLibrary.getLicense(altLicName, licVer, ""); if (libLic != null && !libLic.isDownloadable()) { String type = libLic.getType(); if (type != null) { licName = type; } } } catch (CodeLicenseException cle) { } } } // Still no go! Just create a new config and use what we have. Please note that if the // license was not found among the already known third party licenses, but found in a // license library the license name have been modified to the official name in the license // library. if (tplConfig == null) { tplConfig = new ThirdpartyLicenseConfig(); tplConfig.setType(licName); tplConfig.setVersion(licVer); tplConfig.setLicenseUrl(depPom.getLicenseUrl()); thirdpartyLicenses.addLicense(tplConfig); } // Check if current artifact already exists boolean artifactExists = false; for (ProductConfig prodConfig : tplConfig.getProducts().getProducts()) { if (prodConfig.getName().trim().toLowerCase() .equals(newProdConfig.getName().trim().toLowerCase())) { artifactExists = true; } } // If not add it. if (!artifactExists) { tplConfig.getProducts().addProduct(newProdConfig); } } else { if (lookupArtifactInConfiguredThirdpartyProduct(thirdpartyLicenses, depArtifact) == null) { log.warn("WARNING: Artifact '" + depArtifact + "' has no license information and has not been configured " + "under the <thirdpartyLicenses><license><products> section!"); } } } catch (IOException ioe) { log.warn("WARNING: Failed to extract information from maven dependency: " + depArtifact.getArtifactId() + "-" + depArtifact.getVersion() + " [" + ioe.getMessage() + "]"); } } } } }
From source file:uk.ac.ox.oucs.plugins.DiscoverMojo.java
License:Apache License
protected void deployAndDependents(Set<Artifact> artifacts) throws MojoExecutionException, MojoFailureException { loadCachedImplentations();//from www. ja va 2 s .c o m try { toDeploy.addAll(artifacts); do { ArtifactResolutionResult arr = customResolver.resolveTransitively(artifacts, project.getArtifact(), localRepository, remoteRepositories, customMetadataSource, null); Set<Artifact> resolvedArtifacts = arr.getArtifacts(); Set<ResolutionNode> arrRes = arr.getArtifactResolutionNodes(); for (ResolutionNode node : arrRes) { getLog().info(node.getArtifact().getArtifactId()); for (String artifactId : (List<String>) node.getDependencyTrail()) { getLog().info(" +" + artifactId); } } Set<Artifact> artifactsToFind = new HashSet<Artifact>(); for (Artifact artifact : resolvedArtifacts) { if (needsImplementation(artifact)) { getLog().debug("Needed : " + artifact.toString() + " " + artifact.getDependencyTrail()); artifactsToFind.add(artifact); } else { getLog().debug("Ignored : " + artifact.toString() + " " + artifact.getDependencyTrail()); } } artifacts = new HashSet<Artifact>(); for (Artifact artifact : artifactsToFind) { String artifactKey = artifact.getGroupId() + ":" + artifact.getArtifactId(); if (!checkedArtifacts.contains(artifactKey)) { toDeploy.add(artifact); MavenProject project = findImplementation(artifact); if (project != null) { getLog().info("Found implementation: " + artifactKey + " to " + project.getGroupId() + ":" + project.getArtifactId() + ":" + project.getVersion()); Set<Artifact> projectArtifacts = project.createArtifacts(customArtifactFactory, null, null); //artifacts.addAll(projectArtifacts); if (shouldExpand(project)) { toDeploy.addAll(projectArtifacts); } artifacts.add(project.getArtifact()); toDeploy.add(project.getArtifact()); } else { getLog().info("Unresolved implementation: " + artifactKey); } checkedArtifacts.add(artifactKey); } } } while (artifacts.size() > 0); } catch (InvalidDependencyVersionException e1) { throw new MojoExecutionException("Failed to create artifacts", e1); } catch (ArtifactResolutionException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ArtifactNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { saveCachedImplmentations(); } addToDependencies(); }
From source file:uk.ac.ox.oucs.plugins.DiscoverMojo.java
License:Apache License
private MavenProject locateImplmentation(String groupId, String artifactId, String version) { MavenProject project = null; try {/* w w w.j av a 2 s . c o m*/ Artifact implProjectArtifact = customArtifactFactory.createProjectArtifact(groupId, artifactId, version); // Check we can find it. customResolver.resolve(implProjectArtifact, remoteRepositories, localRepository); project = mavenProjectBuilder.buildFromRepository(implProjectArtifact, remoteRepositories, localRepository); // Projects built from the repository don't have our artifact handler. // Things all blow up if we try to have a custom MavenProjectBuilder component. project.getArtifact() .setArtifactHandler(new CustomArtifactHandlerManager.MissingDependencyArtifactHandler( project.getArtifact().getArtifactHandler())); } catch (ArtifactNotFoundException anfe) { } catch (ArtifactResolutionException are) { } catch (ProjectBuildingException pbe) { } return project; }
From source file:uk.co.unclealex.executable.plugin.ExecutableMojo.java
License:Apache License
/** * Deletes file-sets in the following project build directory order: (source) * directory, output directory, test directory, report directory, and then the * additional file-sets./*w w w.j a v a 2s.c o m*/ * * @throws MojoExecutionException * When a directory failed to get deleted. * @see org.apache.maven.plugin.Mojo#execute() */ public void execute() throws MojoExecutionException { Scripter scripter = Guice.createInjector(new CodeGeneratorModule()).getInstance(Scripter.class); Path buildDirectory = getBuildDirectory().toPath(); Path classesDirectory = getOutputDirectory().toPath(); MavenProject project = getProject(); String version = project.getVersion(); Path targetPath = buildDirectory.resolve(project.getArtifactId() + "-" + version + ".sh"); Path workDirectory = buildDirectory.resolve("executable"); Iterable<Path> dependencyJarFiles = Iterables.transform(getClasspathElements(), new PathFunction()); try { scripter.generate(targetPath, classesDirectory, getHomeExpression(), version, workDirectory, dependencyJarFiles); //getProjectHelper(). attachArtifact(project, "sh", targetPath.toFile()); project.getArtifact().setFile(targetPath.toFile()); } catch (IOException | ExecutableScanException e) { throw new MojoExecutionException("Building an executable script failed.", e); } }