public static CarnotzetModuleCoordinates fromPom(@NonNull Path pom) { Model result; try { BufferedReader in = new BufferedReader(Files.newBufferedReader(pom, StandardCharsets.UTF_8)); MavenXpp3Reader reader = new MavenXpp3Reader(); result = reader.read(in); } catch (XmlPullParserException | IOException e) { throw new CarnotzetDefinitionException(e); } String groupId = result.getGroupId(); String version = result.getVersion(); if (groupId == null) { groupId = result.getParent().getGroupId(); } if (version == null) { version = result.getParent().getVersion(); } return new CarnotzetModuleCoordinates(groupId, result.getArtifactId(), version, null); }
private static String getAppAssemblerBooterVersion() throws IOException, XmlPullParserException { if ( appassemblerVersion == null ) { MavenXpp3Reader reader = new MavenXpp3Reader(); FileReader fileReader = new FileReader( getTestFile( "pom.xml" ) ); try { appassemblerVersion = reader.read( fileReader ).getParent().getVersion(); } finally { IOUtil.close( fileReader ); } } return appassemblerVersion; }
AbstractProjectStub(String pomResourceName) throws IOException, XmlPullParserException { super(new MavenXpp3Reader().read(ITCompileBundleMojo.class.getResourceAsStream(pomResourceName))); Model model = getModel(); setGroupId(model.getGroupId()); setArtifactId(model.getArtifactId()); setVersion(model.getVersion()); setName(model.getName()); setUrl(model.getUrl()); setPackaging(model.getPackaging()); SimpleArtifactStub artifact = new SimpleArtifactStub( model.getGroupId(), model.getArtifactId(), model.getVersion(), model.getPackaging()); artifact.setArtifactHandler(new SimpleArtifactHandlerStub(model.getPackaging())); setArtifact(artifact); Build build = new Build(); build.setFinalName(model.getArtifactId() + '-' + model.getVersion()); setBuild(build); for (Dependency dependency: model.getDependencies()) { if (dependency.getScope() == null) { dependency.setScope(JavaScopes.COMPILE); } } }
/** * Load a Maven {@link Model} object from a POM file. * * @param pom * @param logger * @return the model parsed from the POM file * @throws XmlPullParserException * @throws IOException */ public static Model getModelFromPOM(File pom, Log logger) throws IOException, XmlPullParserException { Model model = null; FileInputStream fis = null; InputStreamReader isr = null; try { fis = new FileInputStream(pom); isr = new InputStreamReader(fis, "utf-8"); // FIXME MavenXpp3Reader reader = new MavenXpp3Reader(); model = reader.read(isr); } finally { try { isr.close(); fis.close(); } catch (IOException e) { e.printStackTrace(); } } return model; }
/** * Merge a Maven {@link Model} object from a POM file to an existing Maven * {@link Model} object. * * @param pom * @param existingModel * @param logger * @return the existing model merged with the parsed model from the POM file * @throws XmlPullParserException * @throws IOException */ public static Model mergeModelFromPOM(File pom, Model existingModel, Log logger) throws IOException, XmlPullParserException { if (pom == null || !pom.exists() || existingModel == null || logger == null) return null; Model model = null; FileInputStream fis = null; InputStreamReader isr = null; try { fis = new FileInputStream(pom); isr = new InputStreamReader(fis, "utf-8"); // FIXME MavenXpp3Reader reader = new MavenXpp3Reader(); model = reader.read(isr); DefaultInheritanceAssembler assembler = new DefaultInheritanceAssembler(); assembler.assembleModelInheritance(model, existingModel, null, null); } finally { try { isr.close(); fis.close(); } catch (IOException e) { e.printStackTrace(); } } return model; }
public MavenProject mavenProjectFor(File pom, List<Property> properties) throws Exception { MavenXpp3Reader reader = new MavenXpp3Reader(); Model model = reader.read(new FileReader(pom)); for (Property p : properties) { model.addProperty(p.name, p.value); } model.setRepositories(new ArrayList<Repository>()); MavenProject project = new MavenProject(model); project.setPluginArtifactRepositories(new ArrayList<ArtifactRepository>()); project.setFile(pom); return project; }
public static String getMavenPropertyVersionFor(String key) { // Maven compile uses a jar, and is in target String basePath = Paths.get("").toAbsolutePath().toString(); if (basePath.endsWith("target")) { basePath = basePath.replace("target/", ""); } String pomfile = basePath + "/../../pom.xml"; Model model = null; MavenXpp3Reader mavenreader = new MavenXpp3Reader(); try { FileReader reader = new FileReader(pomfile); model = mavenreader.read(reader); model.setPomFile(new File(pomfile)); } catch (Exception ex) { throw new IllegalStateException("Can't find the pom.xml file. basePath=" + basePath + " pomFile=" + pomfile); } MavenProject project = new MavenProject(model); Properties properties = project.getProperties(); String value = (String) properties.get(key); return value; }
MavenProject readProject(File pomFile) throws IOException { MavenXpp3Reader mavenReader = new MavenXpp3Reader(); FileReader fileReader = null; try { fileReader = new FileReader(pomFile); Model model = mavenReader.read(fileReader); model.setPomFile(pomFile); MavenProject project = new MavenProject(model); project.setFile(pomFile); project.setArtifact(createArtifact(pomFile, model.getGroupId(), model.getArtifactId(), model.getVersion(), "compile", model.getPackaging(), "")); return project; } catch (Exception e) { throw new RuntimeException(e); } finally { if (fileReader != null) { fileReader.close(); } } }
public void parseParentPom(InputStream inputStream) { try { MavenXpp3Reader reader = new MavenXpp3Reader(); Model model = reader.read(inputStream); PomInformation parentPom = new PomInformation(); setPropertiesFromModel(parentPom, model); parentPom.setParent(model.getParent()); this.parentPomInformation = parentPom; } catch (Exception ex) { LOGGER.error("Error while reading parent POM file", ex); } }
/** * Gets the current raw model before any interpolation what-so-ever. * * @param modifiedPomXMLEventReader The {@link ModifiedPomXMLEventReader} to get the raw model for. * @return The raw model. * @throws IOException if the file is not found or if the file does not parse. */ public static Model getRawModel( ModifiedPomXMLEventReader modifiedPomXMLEventReader ) throws IOException { StringReader stringReader = null; try { stringReader = new StringReader( modifiedPomXMLEventReader.asStringBuilder().toString() ); MavenXpp3Reader reader = new MavenXpp3Reader(); return reader.read( stringReader ); } catch ( XmlPullParserException e ) { IOException ioe = new IOException( e.getMessage() ); ioe.initCause( e ); throw ioe; } finally { if ( stringReader != null ) { stringReader.close(); } } }
protected MavenProject loadProject(File pomFile) throws IOException, XmlPullParserException { MavenProject ret; MavenXpp3Reader mavenReader = new MavenXpp3Reader(); //Removed null and file exists protections that mask errors FileReader reader = null; reader = new FileReader(pomFile); Model model = mavenReader.read(reader); model.setPomFile(pomFile); ret = new MavenProject(model); if(model.getParent() != null) { MavenProject parent = new MavenProject(); parent.setGroupId(model.getParent().getGroupId()); parent.setArtifactId(model.getParent().getArtifactId()); parent.setVersion(model.getParent().getVersion()); ret.setParent(parent); } reader.close(); return ret; }
/** * * @return */ private static String getProjectVersion() { File corePomfile = new File( ITHelper.class.getClassLoader().getResource("").getPath() + "/../../pom.xml"); Model model = null; FileReader reader; MavenXpp3Reader mavenReader = new MavenXpp3Reader(); try { reader = new FileReader(corePomfile); model = mavenReader.read(reader); model.setPomFile(corePomfile); } catch(Exception ex){} MavenProject project = new MavenProject(model); return project.getVersion(); }
/** * Gets current project version from pom.xml file. * * @return Current project version. * @throws MojoFailureException */ protected String getCurrentProjectVersion() throws MojoFailureException { try { // read pom.xml final MavenXpp3Reader mavenReader = new MavenXpp3Reader(); final FileReader fileReader = new FileReader(mavenSession .getCurrentProject().getFile().getAbsoluteFile()); try { final Model model = mavenReader.read(fileReader); if (model.getVersion() == null) { throw new MojoFailureException( "Cannot get current project version. This plugin should be executed from the parent project."); } return model.getVersion(); } finally { if (fileReader != null) { fileReader.close(); } } } catch (Exception e) { throw new MojoFailureException("", e); } }
private Model readMavenModel(String baseDir) throws FileNotFoundException, IOException { Reader reader = null; try { Path pomPath = fs.getPath(baseDir, "pom.xml"); if (Files.notExists(pomPath)) { return null; } reader = new FileReader(pomPath.toFile()); MavenXpp3Reader xpp3Reader = new MavenXpp3Reader(); return xpp3Reader.read(reader); } catch (XmlPullParserException e) { throw new IllegalStateException("Cannot read maven model"); } finally { if (reader != null) { reader.close(); } } }
public String getBuildStringFromPomXml() { Model model = null; FileReader reader = null; MavenXpp3Reader mavenreader = new MavenXpp3Reader(); try { File pomfile = new File("pom.xml"); reader = new FileReader(pomfile); model = mavenreader.read(reader); model.setPomFile(pomfile); } catch (Exception ex) { ex.printStackTrace(); } MavenProject project = new MavenProject(model); logger.trace("version " + project.getVersion()); return project.getVersion(); }
/** * @param pomURL * @return * @throws IOException * @throws XmlPullParserException */ private Model readMavenModel(URL pomURL) throws IOException, XmlPullParserException { Model model = null; InputStream input = null; try { input = pomURL.openStream(); model = new MavenXpp3Reader().read(new InputStreamReader(input)); } finally { IOUtil.close(input); } return model; }
private Model read( Reader reader, boolean strict, InputSource source ) throws IOException { try { if ( source != null ) { return new MavenXpp3ReaderEx().read( reader, strict, source ); } else { return new MavenXpp3Reader().read( reader, strict ); } } catch ( XmlPullParserException e ) { throw new ModelParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e ); } }
public static List<Artifact> resolveDependenciesFromMultimodulePrj(List<String> pomsPaths) { MavenXpp3Reader reader = new MavenXpp3Reader(); List<Artifact> deps = new ArrayList<>(); try { for (String pomx : pomsPaths) { Path pom = Paths.get(pomx); Model model = reader.read(new ByteArrayInputStream(Files.readAllBytes(pom))); if (model.getDependencyManagement() != null && model.getDependencyManagement().getDependencies() != null) { createArtifacts(model.getDependencyManagement().getDependencies(), deps); } if (model.getDependencies() != null) { createArtifacts(model.getDependencies(), deps); } } } catch (Exception ex) { logger.error(ex.getMessage()); return Collections.emptyList(); } return deps; }
private Parent getParent(IDocument doc) { Model model = null; MavenXpp3Reader mavenreader = new MavenXpp3Reader(); InputStream in = null; try { in = IOUtils.toInputStream(doc.get(), "UTF-8"); model = mavenreader.read(in); return model.getParent(); } catch (Exception ex) { } finally { if (in != null) { try { in.close(); } catch (IOException e) { } } } return null; }
public Model getModel(File artifactFile) throws IOException { Model model = null; FileReader reader = null; MavenXpp3Reader mavenreader = new MavenXpp3Reader(); try { reader = new FileReader(artifactFile); model = mavenreader.read(reader); } catch (Exception ex) { } finally { if (reader != null) { reader.close(); } } return model; }
private Model readModelFromPom(File pomFile) throws IOException, XmlPullParserException { FileReader reader = new FileReader(pomFile); try { return new MavenXpp3Reader().read(reader); } finally { reader.close(); } }
private void loadPomVariables() { logger.debug("Loading maven pom.xml variables"); MavenXpp3Reader reader = new MavenXpp3Reader(); try { Model model = reader.read(new FileReader("pom.xml")); ARTIFACT_ID = model.getArtifactId(); VERSION = model.getVersion(); NAME = model.getName(); GROUP_ID = model.getGroupId(); } catch (IOException | XmlPullParserException e) { e.printStackTrace(); } }
private Model getPomModel(File pomFile ) throws IOException, XmlPullParserException { FileInputStream inputStream = null; try { MavenXpp3Reader reader = new MavenXpp3Reader(); inputStream = new FileInputStream(pomFile); return reader.read(inputStream); } finally { if (inputStream != null) inputStream.close(); } }
public BuildConfigurator useSurefireVersion(String version) { File pomFile = new File(projectBuilder.getRoot().toAbsolutePath().toString()+ File.separator + "pom.xml"); try { Model model = new MavenXpp3Reader().read(new FileInputStream(pomFile)); model.getProperties().setProperty("version.surefire.plugin", version); new MavenXpp3Writer().write(new FileOutputStream(pomFile), model); } catch (IOException | XmlPullParserException e) { throw new RuntimeException("Failed setting surefire version in pom.", e); } return this; }
@Given("^POM file \"([^\"]*)\":$") public void pomFile(String path, String body) throws Throwable { Path file = projectDir.resolve(path); Files.createDirectories(file.getParent()); aFile(file.toString(), body); new MavenXpp3Reader().read(Files.newBufferedReader(file)); }
public ParentData(File parentPom) { try { parent = new Parent(); Model model = new MavenXpp3Reader().read(new FileReader(parentPom)); parent.setVersion(model.getVersion()); parent.setArtifactId(model.getArtifactId()); parent.setGroupId(model.getGroupId()); } catch (Exception e) { parent = null; } }
private EnvVars collectEnvVars(Run<?, ?> build, FilePath workspace, TaskListener listener) throws IOException, InterruptedException, XmlPullParserException { EnvVars envVars = build.getEnvironment(listener); FilePath filePath = workspace.child("pom.xml"); if (filePath.exists()) { Properties mavenProperties = new MavenXpp3Reader().read(filePath.read()).getProperties(); for (Map.Entry<Object, Object> entry : mavenProperties.entrySet()) { envVars.put(entry.getKey().toString(), entry.getValue().toString()); } } return envVars; }
public static Model getModel(InputStream is) { final MavenXpp3Reader reader = new MavenXpp3Reader(); try { return reader.read(is); } catch (IOException | XmlPullParserException e) { throw new IllegalStateException(e); } }
private Optional<Model> loadModel(InputStream in) { MavenXpp3Reader reader = new MavenXpp3Reader(); try { Model model = reader.read(in); return Optional.of(model); } catch (Exception e) { return Optional.absent(); } finally { Closeables.closeQuietly(in); } }
static Model loadPomModel(File pomFile) { try { MavenXpp3Reader reader = new MavenXpp3Reader(); try (FileReader pomReader = new FileReader(pomFile)) { return reader.read(pomReader); } } catch (Exception e) { throw new ProjectCannotStartException("Error while reading maven meta data", e); } }
public static void main(String[] args) throws URISyntaxException, IOException, ParseException, NoSuchMethodException, XmlPullParserException { MavenXpp3Reader mavenXpp3Reader = new MavenXpp3Reader(); Model model = mavenXpp3Reader.read(newBufferedReader(Paths.get("pom.xml"), UTF_8)); WikiGenerator wikiGenerator = new WikiGenerator(model.getGroupId(), model.getArtifactId(), model.getVersion()); if (wikiGenerator.weAreReleasing()) { wikiGenerator.copyDocsToReleaseDirectory(); } wikiGenerator.generateCurrentDocumentation(); wikiGenerator.generateVersionsIndexPage(); }
public static List<License> getLicenses(File filePath, String userSettings, String globalSettings) { try { Model model = new MavenXpp3Reader().read(new FileInputStream(filePath)); if (!model.getLicenses().isEmpty()) { return model.getLicenses(); } else { if (model.getParent() != null) { Parent parent = model.getParent(); Dependency dependency = new Dependency(parent.getGroupId() + ":" + parent.getArtifactId(), parent.getVersion(), null); return getLicenses(DirectoryFinder.getPomPath(dependency, DirectoryFinder.getMavenRepsitoryDir(userSettings, globalSettings)), userSettings, globalSettings); } else { return Collections.emptyList(); } } } catch (Exception e) { LOGGER.warn("Could not parse Maven POM " + filePath, e); return Collections.emptyList(); } }
private int getThreadCount() throws JDOMException, IOException, XmlPullParserException { int threadCount=0; MavenXpp3Reader reader = new MavenXpp3Reader(); Model model = reader.read(new FileReader(pomFile)); ArrayList<Plugin> pluginsList = (ArrayList<Plugin>) model.getBuild().getPlugins(); for (Plugin plugin: pluginsList) { if(plugin.getArtifactId().equals("cucumber-runner-maven-plugin")){ Xpp3Dom xpp = (Xpp3Dom) plugin.getConfiguration(); threadCount= Integer.parseInt(xpp.getChild("threadCount").getValue()); } } return threadCount; }
@Before public void before() throws Exception { reporter = new OutputStreamEventReporter(writer); spy = new JenkinsMavenEventSpy(reporter) { @Override protected boolean isEventSpyDisabled() { return false; } }; spy.init(new EventSpy.Context() { @Override public Map<String, Object> getData() { return new HashMap(); } }); MavenXpp3Reader mavenXpp3Reader = new MavenXpp3Reader(); InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("org/jenkinsci/plugins/pipeline/maven/eventspy/pom.xml"); Assert.assertThat(in, CoreMatchers.notNullValue()); Model model = mavenXpp3Reader.read(in); project = new MavenProject(model); project.setGroupId(model.getGroupId()); project.setArtifactId(model.getArtifactId()); project.setVersion(model.getVersion()); project.setName(model.getName()); }
private JenkinsMavenEventSpy createSpy() throws Exception { FileMavenEventReporter reporter = new FileMavenEventReporter(); JenkinsMavenEventSpy spy = new JenkinsMavenEventSpy(reporter) { @Override protected boolean isEventSpyDisabled() { return false; } }; spy.init(new EventSpy.Context() { @Override public Map<String, Object> getData() { return new HashMap(); } }); MavenXpp3Reader mavenXpp3Reader = new MavenXpp3Reader(); InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("org/jenkinsci/plugins/pipeline/maven/eventspy/pom.xml"); Assert.assertThat(in, CoreMatchers.notNullValue()); Model model = mavenXpp3Reader.read(in); project = new MavenProject(model); project.setGroupId(model.getGroupId()); project.setArtifactId(model.getArtifactId()); project.setVersion(model.getVersion()); project.setName(model.getName()); return spy; }
/** * This method does not take into account inheritance of the parent and interpolation of expressions. * * For a more complete model building see: * * @see Main#complexModelBuilding(File) */ private static Model simpleModelBuilding(File file) throws Exception { MavenXpp3Reader reader = new MavenXpp3Reader(); //noinspection UnnecessaryLocalVariable Model model = reader.read(new FileReader(file)); return model; }
/** * Default constructor */ public ProjectStub() { MavenXpp3Reader pomReader = new MavenXpp3Reader(); Model model; try { model = pomReader.read(ReaderFactory.newXmlReader(new File(getBasedir(), "pom.xml"))); setModel(model); } catch (Exception e) { throw new RuntimeException(e); } setGroupId(model.getGroupId()); setArtifactId(model.getArtifactId()); setVersion(model.getVersion()); setName(model.getName()); setUrl(model.getUrl()); setPackaging(model.getPackaging()); Build build = new Build(); build.setFinalName(model.getArtifactId()); build.setDirectory(getBasedir() + "/target"); build.setSourceDirectory(getBasedir() + "/src/main/java"); build.setOutputDirectory(getBasedir() + "/target/classes"); build.setTestSourceDirectory(getBasedir() + "/src/test/java"); build.setTestOutputDirectory(getBasedir() + "/target/test-classes"); setBuild(build); List<String> compileSourceRoots = new ArrayList<String>(); compileSourceRoots.add(getBasedir() + "/src/main/java"); setCompileSourceRoots(compileSourceRoots); List<String> testCompileSourceRoots = new ArrayList<String>(); testCompileSourceRoots.add(getBasedir() + "/src/test/java"); setTestCompileSourceRoots(testCompileSourceRoots); }
private static Model readModel(final File pomXmlFile, final Model preModel) throws IOException, XmlPullParserException { final Reader reader; if (preModel == null) { reader = new FileReader(pomXmlFile); } else { final File varReplacedPomXmlFile = replaceVars(pomXmlFile, preModel); reader = new FileReader(varReplacedPomXmlFile); } final Model model; try { final MavenXpp3Reader xpp3Reader = new MavenXpp3Reader(); model = xpp3Reader.read(reader); } finally { reader.close(); } if (model.getVersion() == null) { if (model.getParent() == null) { throw new IllegalStateException( "Version of model is null and no parent is avaliable: " + pomXmlFile); } if (model.getParent().getVersion() == null) { throw new IllegalStateException("Version of parent is null: " + pomXmlFile); } model.setVersion(model.getParent().getVersion()); if (model.getVersion() == null) { throw new IllegalStateException("Parent as also no version: " + pomXmlFile); } } return model; }
private static void appInit() { // Application name and version from POM MavenXpp3Reader reader = new MavenXpp3Reader(); try { if ((new File("pom.xml")).exists()) { model = reader.read(new FileReader("pom.xml")); } else { model = reader.read(new InputStreamReader( JarExplorer.class.getResourceAsStream("/META-INF/maven/org.jarexplorer/jarexplorer/pom.xml"))); } } catch (Exception e) { // something went wrong with reading the pom, just use a default // e.printStackTrace(); model = new Model(); model.setName("JarExplorer"); model.setVersion("unknown version"); } APP_NAME = model.getName() + " " + model.getVersion(); // Configuration setup Configuration.setFileName(System.getProperty("user.home") + "/" + ".JarExplorer.properties", model.getName()); // Configuration defaults if (Configuration.getProperty(CONFIG_ZIP_EXTENSIONS) == null) { Configuration.setProperty(CONFIG_ZIP_EXTENSIONS, "jar,zip,war,ear,rar"); } }