use of org.apache.maven.model.io.xpp3.MavenXpp3Reader in project maven-plugins by apache.
the class SignAndDeployFileMojo method readModel.
/**
* Extract the model from the specified POM file.
*
* @param pomFile The path of the POM file to parse, must not be <code>null</code>.
* @return The model from the POM file, never <code>null</code>.
* @throws MojoExecutionException If the file doesn't exist of cannot be read.
*/
private Model readModel(File pomFile) throws MojoExecutionException {
Reader reader = null;
try {
reader = ReaderFactory.newXmlReader(pomFile);
final Model model = new MavenXpp3Reader().read(reader);
reader.close();
reader = null;
return model;
} catch (FileNotFoundException e) {
throw new MojoExecutionException("POM not found " + pomFile, e);
} catch (IOException e) {
throw new MojoExecutionException("Error reading POM " + pomFile, e);
} catch (XmlPullParserException e) {
throw new MojoExecutionException("Error parsing POM " + pomFile, e);
} finally {
IOUtil.close(reader);
}
}
use of org.apache.maven.model.io.xpp3.MavenXpp3Reader in project maven-plugins by apache.
the class InstallFileMojo method readModel.
/**
* Parses a POM.
*
* @param pomFile The path of the POM file to parse, must not be <code>null</code>.
* @return The model from the POM file, never <code>null</code>.
* @throws MojoExecutionException If the POM could not be parsed.
*/
private Model readModel(File pomFile) throws MojoExecutionException {
Reader reader = null;
try {
reader = ReaderFactory.newXmlReader(pomFile);
final Model model = new MavenXpp3Reader().read(reader);
reader.close();
reader = null;
return model;
} catch (FileNotFoundException e) {
throw new MojoExecutionException("File not found " + pomFile, e);
} catch (IOException e) {
throw new MojoExecutionException("Error reading POM " + pomFile, e);
} catch (XmlPullParserException e) {
throw new MojoExecutionException("Error parsing POM " + pomFile, e);
} finally {
IOUtil.close(reader);
}
}
use of org.apache.maven.model.io.xpp3.MavenXpp3Reader in project maven-plugins by apache.
the class BundlePackMojo method readPom.
/**
* Read the POM file.
*
* @param pom The file to read
* @return A Maven Model
* @throws MojoExecutionException if something goes wrong when reading the file
*/
private Model readPom(File pom) throws MojoExecutionException {
Model model;
XmlStreamReader reader = null;
try {
reader = ReaderFactory.newXmlReader(pom);
model = new MavenXpp3Reader().read(reader);
reader.close();
reader = null;
} catch (XmlPullParserException e) {
throw new MojoExecutionException("Unable to parse POM at " + pom.getAbsolutePath() + ": " + e.getMessage(), e);
} catch (FileNotFoundException e) {
throw new MojoExecutionException("Unable to read POM at " + pom.getAbsolutePath() + ": " + e.getMessage(), e);
} catch (IOException e) {
throw new MojoExecutionException("Unable to read POM at " + pom.getAbsolutePath() + ": " + e.getMessage(), e);
} finally {
IOUtil.close(reader);
}
return model;
}
use of org.apache.maven.model.io.xpp3.MavenXpp3Reader in project maven-archetype by apache.
the class ArchetypeTest method testArchetype.
public void testArchetype() throws Exception {
FileUtils.deleteDirectory(getTestFile("target/quickstart"));
// ----------------------------------------------------------------------
// This needs to be encapsulated in a maven test case.
// ----------------------------------------------------------------------
ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) getContainer().lookup(ArtifactRepositoryLayout.ROLE);
String mavenRepoLocal = getTestFile("target/local-repository").toURI().toURL().toString();
ArtifactRepository localRepository = new DefaultArtifactRepository("local", mavenRepoLocal, layout);
List<ArtifactRepository> remoteRepositories = new ArrayList<ArtifactRepository>();
String mavenRepoRemote = getTestFile("src/test/repository").toURI().toURL().toString();
ArtifactRepository remoteRepository = new DefaultArtifactRepository("remote", mavenRepoRemote, layout);
remoteRepositories.add(remoteRepository);
ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
buildingRequest.setRemoteRepositories(remoteRepositories);
MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManager(localRepository.getBasedir()));
buildingRequest.setRepositorySession(repositorySession);
ArchetypeGenerationRequest request = new ArchetypeGenerationRequest().setProjectBuildingRequest(buildingRequest).setPackage("org.apache.maven.quickstart").setGroupId("maven").setArtifactId("quickstart").setVersion("1.0-alpha-1-SNAPSHOT").setArchetypeGroupId("org.apache.maven.archetypes").setArchetypeArtifactId("maven-archetype-quickstart").setArchetypeVersion("1.0-alpha-1-SNAPSHOT").setLocalRepository(localRepository).setRemoteArtifactRepositories(remoteRepositories).setOutputDirectory(getTestFile("target").getAbsolutePath());
// parameters.put( "name", "jason" );
archetype.createArchetype(request, remoteRepository);
// ----------------------------------------------------------------------
// Set up the Velocity context
// ----------------------------------------------------------------------
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("basedir", request.getOutputDirectory());
parameters.put("package", request.getPackage());
parameters.put("packageName", request.getPackage());
parameters.put("groupId", request.getGroupId());
parameters.put("artifactId", request.getArtifactId());
parameters.put("version", request.getVersion());
Context context = new VelocityContext();
for (Map.Entry<String, Object> entry : parameters.entrySet()) {
context.put(entry.getKey(), entry.getValue());
}
// ----------------------------------------------------------------------
// Validate POM generation
// ----------------------------------------------------------------------
ArtifactFactory artifactFactory = (ArtifactFactory) lookup(ArtifactFactory.class.getName());
Artifact archetypeArtifact = artifactFactory.createArtifact(request.getArchetypeGroupId(), request.getArchetypeArtifactId(), request.getArchetypeVersion(), Artifact.SCOPE_RUNTIME, "jar");
StringWriter writer = new StringWriter();
ClassLoader old = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(getContextClassloader(archetypeArtifact, localRepository, remoteRepositories));
try {
VelocityComponent velocity = (VelocityComponent) lookup(VelocityComponent.class.getName());
velocity.getEngine().mergeTemplate(OldArchetype.ARCHETYPE_RESOURCES + "/" + OldArchetype.ARCHETYPE_POM, context, writer);
} finally {
Thread.currentThread().setContextClassLoader(old);
}
Model generatedModel, templateModel;
try {
StringReader strReader = new StringReader(writer.toString());
MavenXpp3Reader reader = new MavenXpp3Reader();
templateModel = reader.read(strReader);
} catch (IOException e) {
throw new ArchetypeTemplateProcessingException("Error reading template POM", e);
}
File artifactDir = getTestFile("target", (String) parameters.get("artifactId"));
File pomFile = getTestFile(artifactDir.getAbsolutePath(), OldArchetype.ARCHETYPE_POM);
try {
FileReader pomReader = new FileReader(pomFile);
MavenXpp3Reader reader = new MavenXpp3Reader();
generatedModel = reader.read(pomReader);
} catch (IOException e) {
throw new ArchetypeTemplateProcessingException("Error reading generated POM", e);
} catch (XmlPullParserException e) {
throw new ArchetypeTemplateProcessingException("Error reading generated POM", e);
}
assertEquals("Generated POM ArtifactId is not equivalent to expected result.", generatedModel.getArtifactId(), templateModel.getArtifactId());
assertEquals("Generated POM GroupId is not equivalent to expected result.", generatedModel.getGroupId(), templateModel.getGroupId());
assertEquals("Generated POM Id is not equivalent to expected result.", generatedModel.getId(), templateModel.getId());
assertEquals("Generated POM Version is not equivalent to expected result.", generatedModel.getVersion(), templateModel.getVersion());
assertEquals("Generated POM Packaging is not equivalent to expected result.", generatedModel.getPackaging(), templateModel.getPackaging());
assertEquals("Generated POM Developers is not equivalent to expected result.", generatedModel.getDevelopers(), templateModel.getDevelopers());
assertEquals("Generated POM Scm is not equivalent to expected result.", generatedModel.getScm(), templateModel.getScm());
}
use of org.apache.maven.model.io.xpp3.MavenXpp3Reader in project incubator-servicecomb-java-chassis by apache.
the class Deploys method initPomVersion.
private void initPomVersion() throws Throwable {
// already set manually
if (pomVersion != null) {
return;
}
// already package to jar
pomVersion = Deploys.class.getPackage().getImplementationVersion();
if (pomVersion != null) {
return;
}
// run in ide
MavenXpp3Reader reader = new MavenXpp3Reader();
Model model = reader.read(new FileReader("pom.xml"));
pomVersion = model.getVersion();
if (pomVersion != null) {
return;
}
if (model.getParent() == null) {
throw new IllegalStateException("can not find pom ServiceComb version");
}
pomVersion = model.getParent().getVersion();
if (pomVersion != null) {
return;
}
throw new IllegalStateException("can not find pom ServiceComb version");
}
Aggregations