List of usage examples for org.apache.maven.artifact.versioning VersionRange createFromVersion
public static VersionRange createFromVersion(String version)
From source file:be.fedict.eid.applet.maven.sql.ddl.SQLDDLMojo.java
License:Open Source License
@Override public void execute() throws MojoExecutionException, MojoFailureException { getLog().info("SQL DDL script generator"); File outputFile = new File(this.outputDirectory, this.outputName); getLog().info("Output SQL DDL script file: " + outputFile.getAbsolutePath()); this.outputDirectory.mkdirs(); try {/*from ww w . j a v a 2 s. com*/ outputFile.createNewFile(); } catch (IOException e) { throw new MojoExecutionException("I/O error.", e); } for (ArtifactItem artifactItem : this.artifactItems) { getLog().info("artifact: " + artifactItem.getGroupId() + ":" + artifactItem.getArtifactId()); List<Dependency> dependencies = this.project.getDependencies(); String version = null; for (Dependency dependency : dependencies) { if (StringUtils.equals(dependency.getArtifactId(), artifactItem.getArtifactId()) && StringUtils.equals(dependency.getGroupId(), artifactItem.getGroupId())) { version = dependency.getVersion(); break; } } getLog().info("artifact version: " + version); VersionRange versionRange = VersionRange.createFromVersion(version); Artifact artifact = this.artifactFactory.createDependencyArtifact(artifactItem.getGroupId(), artifactItem.getArtifactId(), versionRange, "jar", null, Artifact.SCOPE_COMPILE); try { this.resolver.resolve(artifact, this.remoteRepos, this.local); } catch (ArtifactResolutionException e) { throw new MojoExecutionException("Unable to resolve artifact.", e); } catch (ArtifactNotFoundException e) { throw new MojoExecutionException("Unable to find artifact.", e); } getLog().info("artifact file: " + artifact.getFile().getAbsolutePath()); getLog().info("hibernate dialect: " + this.hibernateDialect); URL artifactUrl; try { artifactUrl = artifact.getFile().toURI().toURL(); } catch (MalformedURLException e) { throw new MojoExecutionException("URL error.", e); } URLClassLoader classLoader = new URLClassLoader(new URL[] { artifactUrl }, this.getClass().getClassLoader()); Thread.currentThread().setContextClassLoader(classLoader); AnnotationDB annotationDb = new AnnotationDB(); try { annotationDb.scanArchives(artifactUrl); } catch (IOException e) { throw new MojoExecutionException("I/O error.", e); } Set<String> classNames = annotationDb.getAnnotationIndex().get(Entity.class.getName()); getLog().info("# JPA entity classes: " + classNames.size()); AnnotationConfiguration configuration = new AnnotationConfiguration(); configuration.setProperty("hibernate.dialect", this.hibernateDialect); Dialect dialect = Dialect.getDialect(configuration.getProperties()); getLog().info("dialect: " + dialect.toString()); for (String className : classNames) { getLog().info("JPA entity: " + className); Class<?> entityClass; try { entityClass = classLoader.loadClass(className); getLog().info("entity class loader: " + entityClass.getClassLoader()); } catch (ClassNotFoundException e) { throw new MojoExecutionException("class not found.", e); } configuration.addAnnotatedClass(entityClass); } SchemaExport schemaExport = new SchemaExport(configuration); schemaExport.setFormat(true); schemaExport.setHaltOnError(true); schemaExport.setOutputFile(outputFile.getAbsolutePath()); schemaExport.setDelimiter(";"); try { getLog().info("SQL DDL script: " + IOUtil.toString(new FileInputStream(outputFile))); } catch (FileNotFoundException e) { throw new MojoExecutionException("file not found.", e); } catch (IOException e) { throw new MojoExecutionException("I/O error.", e); } // operate schemaExport.execute(true, false, false, true); List<Exception> exceptions = schemaExport.getExceptions(); for (Exception exception : exceptions) { getLog().error("exception: " + exception.getMessage()); } } }
From source file:com.alibaba.citrus.maven.eclipse.base.ide.IdeUtils.java
License:Apache License
/** * @param artifactIds an array of artifactIds, should not be <code>null</code> * @param dependencies a list of {@link Dependency}-objects, should not be <code>null</code> * @return the resolved ArtifactVersion, otherwise <code>null</code> */// w w w . j a va 2s . c om public static ArtifactVersion getArtifactVersion(String[] artifactIds, List /*<Dependency>*/ dependencies) { for (int j = 0; j < artifactIds.length; j++) { String id = artifactIds[j]; Iterator depIter = dependencies.iterator(); while (depIter.hasNext()) { Dependency dep = (Dependency) depIter.next(); if (id.equals(dep.getArtifactId())) { return VersionRange.createFromVersion(dep.getVersion()).getRecommendedVersion(); } } } return null; }
From source file:com.encodedknowledge.maven.dependency.sanity.ArtifactTestUtils.java
License:Apache License
public static Artifact createArtifact(String artifactString) { String[] parts = artifactString.split(":"); if (parts.length < 3 || parts.length > 4) { throw new IllegalArgumentException( "artifactString must be in the format groupId:artifactId:version[:scope]"); }/*from w ww. j av a 2s . c om*/ String groupId = parts[0]; String artifactId = parts[1]; String version = parts[2]; String scope = parts.length > 3 ? parts[3] : "compile"; return new DefaultArtifact(groupId, artifactId, VersionRange.createFromVersion(version), scope, "jar", "", null); }
From source file:com.github.maven_nar.NarIntegrationTestMojo.java
License:Apache License
private void addProvider(final SurefireBooter surefireBooter, final String provider, final String version, final Artifact filteredArtifact) throws ArtifactNotFoundException, ArtifactResolutionException { final Artifact providerArtifact = this.artifactFactory.createDependencyArtifact("org.apache.maven.surefire", provider, VersionRange.createFromVersion(version), "jar", null, Artifact.SCOPE_TEST); final ArtifactResolutionResult result = resolveArtifact(filteredArtifact, providerArtifact); for (final Object element : result.getArtifacts()) { final Artifact artifact = (Artifact) element; getLog().debug("Adding to surefire test classpath: " + artifact.getFile().getAbsolutePath()); surefireBooter.addSurefireClassPathUrl(artifact.getFile().getAbsolutePath()); }//from www . ja v a2 s . com }
From source file:com.github.panthers.maven.plugins.fromConfig.AbstractFromConfigMojo.java
License:Apache License
/** * For a given artifactItem with range this produces the list of artifactItems with available versions. * Versions are filtered based on configuration * Output file name is set as artifactId-version.type * @param artifactItemsWithRange//ww w . j a v a 2 s . c o m * @return * @throws MojoExecutionException */ protected List<ArtifactItem> getProcessRangedArtifactItems(ArtifactItemsWithRange artifactItemsWithRange) throws MojoExecutionException { getLog().debug(logArtifactWithRange(artifactItemsWithRange)); List<DefaultArtifactVersion> avilableVersions = getAvailableVersions(artifactItemsWithRange); List<ArtifactItem> artifactItems = new ArrayList<ArtifactItem>(); for (DefaultArtifactVersion av : avilableVersions) { Artifact artifactWithVersion; if (StringUtils.isEmpty(artifactItemsWithRange.getClassifier())) { artifactWithVersion = factory.createDependencyArtifact(artifactItemsWithRange.getGroupId(), artifactItemsWithRange.getArtifactId(), VersionRange.createFromVersion(av.toString()), artifactItemsWithRange.getType(), null, Artifact.SCOPE_COMPILE); } else { artifactWithVersion = factory.createDependencyArtifact(artifactItemsWithRange.getGroupId(), artifactItemsWithRange.getArtifactId(), VersionRange.createFromVersion(av.toString()), artifactItemsWithRange.getType(), artifactItemsWithRange.getClassifier(), Artifact.SCOPE_COMPILE); } artifactWithVersion = resolveArtifact(artifactWithVersion); ArtifactItem artifactItem = new ArtifactItem(artifactWithVersion); //Destination name is artifactId-version.type artifactItem.setDestFileName( artifactItem.getArtifactId() + "-" + artifactItem.getVersion() + "." + artifactItem.getType()); //First preference goes to artifactItem output directory if (artifactItemsWithRange.getOutputDirectory() == null) { artifactItem.setOutputDirectory(outputDirectory); } else { artifactItem.setOutputDirectory(artifactItemsWithRange.getOutputDirectory()); } artifactItem.getOutputDirectory().mkdir(); artifactItem.setOverWrite(artifactItemsWithRange.getOverWrite()); artifactItem.setNeedsProcessing(true); getLog().info( "Artifact : " + artifactItemsWithRange.getArtifactId() + " Found version : " + av.toString()); artifactItems.add(artifactItem); } return artifactItems; }
From source file:com.github.wix_maven.LightMojo.java
License:Apache License
/** * Based on Maven-dependency-plugin AbstractFromConfigurationMojo. * //from ww w . j a v a2 s .co m * Resolves the Artifact from the remote repository if necessary. If no version is specified, it will be retrieved from the dependency list or * from the DependencyManagement section of the pom. * * @param artifactItem * containing information about artifact from plugin configuration. * @return Artifact object representing the specified file. * @throws MojoExecutionException * with a message if the version can't be found in DependencyManagement. */ protected Set<Artifact> getRelatedArtifacts(Artifact artifactItem, String arch, String culture) throws MojoExecutionException { Set<Artifact> artifactSet = new HashSet<Artifact>(); // Map managedVersions = createManagedVersionMap( factory, project.getId(), project.getDependencyManagement() ); VersionRange vr; try { vr = VersionRange.createFromVersionSpec(artifactItem.getVersion()); } catch (InvalidVersionSpecificationException e1) { vr = VersionRange.createFromVersion(artifactItem.getVersion()); } if (PACK_LIB.equalsIgnoreCase(artifactItem.getType()) || PACK_MERGE.equalsIgnoreCase(artifactItem.getType())) { boolean hasSomething = true; // even if this module has culture it's base modules may be neutral try { String classifier = arch + "-" + "neutral"; getArtifact(artifactItem.getGroupId(), artifactItem.getArtifactId(), artifactItem.getType(), artifactSet, vr, classifier); } catch (MojoExecutionException e) { if (culture == null) throw e; hasSomething = false; } if (culture != null) { try { String classifier = arch + "-" + getPrimaryCulture(culture); getArtifact(artifactItem.getGroupId(), artifactItem.getArtifactId(), artifactItem.getType(), artifactSet, vr, classifier); } catch (MojoExecutionException e) { if (hasSomething == false) throw e; } } } // list out all the dependencies with their classifiers // if ("msi".equalsIgnoreCase(artifactItem.getType()) ) { // boolean hasSomething = true; // // even if this module has culture it's base modules may be neutral // try { // String classifier = arch + "-" + "neutral"; // getArtifact(artifactItem.getGroupId(), artifactItem.getArtifactId(), artifactItem.getType(), artifactSet, vr, classifier); // } catch (MojoExecutionException e) { // if (culture == null) // throw e; // hasSomething = false; // } // // if (culture != null) { // try { // String classifier = arch + "-" + getPrimaryCulture(culture); // getArtifact(artifactItem.getGroupId(), artifactItem.getArtifactId(), artifactItem.getType(), artifactSet, vr, classifier); // } catch (MojoExecutionException e) { // if (hasSomething == false) // throw e; // } // } // } // // else if ("nar".equalsIgnoreCase(artifactItem.getType())) { // get one of both 32 & 64 bit... how do we tell whats there to use? // go through nar // for (String arch : getPlatforms()) { // for (String culture : cultures) { // String culture = null; // String classifier = arch + "-" + (culture == null ? "neutral" : culture); // getArtifact(artifactItem.getGroupId(), artifactItem.getArtifactId(), artifactItem.getType(), artifactSet, vr, "x86-Windows-msvc-shared"); // } // } return artifactSet; }
From source file:com.github.wix_maven.PatchMojo.java
License:Apache License
/** * Based on Maven-dependency-plugin AbstractFromConfigurationMojo. * //w w w. j a va 2 s . c o m * Resolves the Artifact from the remote repository if necessary. If no version is specified, it will be retrieved from the dependency list or * from the DependencyManagement section of the pom. * * @param artifactItem * containing information about artifact from plugin configuration. * @return Artifact object representing the specified file. * @throws MojoExecutionException * with a message if the version can't be found in DependencyManagement. */ protected Artifact getRelatedArtifact(ArtifactItem artifactItem, String arch, String culture, String type) throws MojoExecutionException { VersionRange vr; try { vr = VersionRange.createFromVersionSpec(artifactItem.getVersion()); } catch (InvalidVersionSpecificationException e1) { vr = VersionRange.createFromVersion(artifactItem.getVersion()); } Set<Artifact> artifactSet = new HashSet<Artifact>(); String classifier = arch + "-" + (culture == null ? "neutral" : getPrimaryCulture(culture)); getArtifact(artifactItem.getGroupId(), artifactItem.getArtifactId(), type, artifactSet, vr, classifier); if (artifactSet.size() != 1) // this is more like an assert - we are only asking for one, and if none it already threw. throw new MojoExecutionException(String.format("Found multiple artifacts for : %1:%2:%3:%4:%5", artifactItem.getGroupId(), artifactItem.getArtifactId(), type, vr, classifier)); return artifactSet.iterator().next(); }
From source file:com.github.wix_maven.UnpackDependenciesMojo.java
License:Apache License
/** * Based on Maven-dependency-plugin AbstractFromConfigurationMojo. * //w ww. ja v a2s . c o m * Resolves the Artifact from the remote repository if necessary. If no version is specified, it will be retrieved from the dependency list or * from the DependencyManagement section of the pom. * * @param artifactItem * containing information about artifact from plugin configuration. * @return Artifact object representing the specified file. * @throws MojoExecutionException * with a message if the version can't be found in DependencyManagement. */ protected Set<Artifact> getRelatedArtifacts(Artifact artifactItem) throws MojoExecutionException { Artifact artifact; Set<Artifact> artifactSet = new HashSet<Artifact>(); // Map managedVersions = createManagedVersionMap( factory, project.getId(), project.getDependencyManagement() ); VersionRange vr; try { vr = VersionRange.createFromVersionSpec(artifactItem.getVersion()); } catch (InvalidVersionSpecificationException e1) { // TODO Auto-generated catch block e1.printStackTrace(); vr = VersionRange.createFromVersion(artifactItem.getVersion()); } // if ("msi".equalsIgnoreCase(artifactItem.getType()) || "msp".equalsIgnoreCase(artifactItem.getType()) // || "wixlib".equalsIgnoreCase(artifactItem.getType()) || "msm".equalsIgnoreCase(artifactItem.getType())) { // // artifactItem.getFile().getWixInfo(); // // if (cultures.isEmpty()) // // cultures.add(null); // // // for (String culture : cultures) { // // boolean hasSomething = true; // // even if this module has culture it's base modules may be neutral // try { // String classifier = arch + "-" + "neutral"; // getArtifact(artifactItem.getGroupId(), artifactItem.getArtifactId(), artifactItem.getType(), artifactSet, vr, classifier); // } catch (MojoExecutionException e) { // if (culture == null) // throw e; // hasSomething = false; // } // // if (culture != null) { // try { // String classifier = arch + "-" + getPrimaryCulture(culture); // getArtifact(artifactItem.getGroupId(), artifactItem.getArtifactId(), artifactItem.getType(), artifactSet, vr, classifier); // } catch (MojoExecutionException e) { // if( hasSomething == false ) // throw e; // } // } // // if ("msi".equalsIgnoreCase(artifactItem.getType()) || "msp".equalsIgnoreCase(artifactItem.getType())) { // String classifier = arch + "-" + (culture == null ? "neutral" : getPrimaryCulture(culture) ); // getArtifact(artifactItem.getGroupId(), artifactItem.getArtifactId(), "wixpdb", artifactSet, vr, classifier); // } // } // if ("nar".equalsIgnoreCase(artifactItem.getType())) { // get one of both 32 & 64 bit... how do we tell whats there to use? // go through nar // for (String arch : getPlatforms()) { // for (String culture : cultures) { // String culture = null; // String classifier = arch + "-" + (culture == null ? "neutral" : culture); // getArtifact(artifactItem, artifactSet, vr, "x86-Windows-msvc-shared"); // } // } return artifactSet; }
From source file:com.googlecode.japi.checker.maven.plugin.BackwardCompatibilityCheckerMojo.java
License:Apache License
/** * Resolves the Artifact from the remote repository if necessary. If no version is specified, it will be retrieved * from the dependency list or from the DependencyManagement section of the pom. * //from www. j a v a 2 s .c om * @param artifactItem containing information about artifact from plugin configuration. * @return Artifact object representing the specified file. * @throws MojoExecutionException with a message if the version can't be found in DependencyManagement. */ protected void updateArtifact(ArtifactItem artifactItem) throws MojoExecutionException { if (artifactItem.getArtifact() != null) { return; } VersionRange vr; try { vr = VersionRange.createFromVersionSpec(artifactItem.getVersion()); } catch (InvalidVersionSpecificationException e) { vr = VersionRange.createFromVersion(artifactItem.getVersion()); } Artifact artifact = getFactory().createDependencyArtifact(artifactItem.getGroupId(), artifactItem.getArtifactId(), vr, artifactItem.getType(), null, Artifact.SCOPE_COMPILE); try { getResolver().resolve(artifact, remoteRepos, localRepository); } catch (ArtifactResolutionException e) { throw new MojoExecutionException("Unable to resolve artifact.", e); } catch (ArtifactNotFoundException e) { throw new MojoExecutionException("Unable to find artifact.", e); } artifactItem.setArtifact(artifact); }
From source file:com.googlecode.t7mp.MyArtifactResolver.java
License:Apache License
/** * resolves an Artifact from the repositorys. * /*from www .jav a2 s . c o m*/ * @param groupId - Artifact GroupId * @param artifactId - Artifact Id * @param version - Artifact Version * @param classifier - Artifact Version * @param type - Artifact Type * @param scope - Artifact Scope * @return * @throws MojoExecutionException */ public Artifact resolve(String groupId, String artifactId, String version, String classifier, String type, String scope) throws MojoExecutionException { Artifact artifact = factory.createDependencyArtifact(groupId, artifactId, VersionRange.createFromVersion(version), type, classifier, Artifact.SCOPE_COMPILE); try { resolver.resolve(artifact, remoteRepositories, local); } catch (ArtifactResolutionException e) { throw new MojoExecutionException(e.getMessage(), e); } catch (ArtifactNotFoundException e) { throw new MojoExecutionException(e.getMessage(), e); } return artifact; }