List of usage examples for org.apache.maven.project MavenProject getBasedir
public File getBasedir()
From source file:org.codehaus.mojo.versions.api.PomHelper.java
License:Apache License
/** * Finds the local root of the specified project. * * @param project The project to find the local root for. * @param localRepository the local repo. * @param globalProfileManager the global profile manager. * @param logger The logger to log to. * @return The local root (note this may be the project passed as an argument). *///from ww w . jav a2 s. c o m public static MavenProject getLocalRoot(MavenProjectBuilder builder, MavenProject project, ArtifactRepository localRepository, ProfileManager globalProfileManager, Log logger) { logger.info("Searching for local aggregator root..."); while (true) { final File parentDir = project.getBasedir().getParentFile(); if (parentDir.isDirectory()) { logger.debug("Checking to see if " + parentDir + " is an aggregator parent"); File parent = new File(parentDir, "pom.xml"); if (parent.isFile()) { try { final MavenProject parentProject = builder.build(parent, localRepository, globalProfileManager); if (getAllChildModules(parentProject, logger).contains(project.getBasedir().getName())) { logger.debug(parentDir + " is an aggregator parent"); project = parentProject; continue; } else { logger.debug(parentDir + " is not an aggregator parent"); } } catch (ProjectBuildingException e) { logger.warn(e); } } } logger.debug("Local aggregation root is " + project.getBasedir()); return project; } }
From source file:org.codehaus.mojo.versions.api.PomHelper.java
License:Apache License
/** * Builds a sub-map of raw models keyed by module path. * * @param path The relative path to base the sub-map on. * @param model The model at the relative path. * @param project The project to build from. * @param logger The logger for logging. * @return A map of raw models keyed by path relative to the project's basedir. * @throws IOException if things go wrong. *//*from w ww . j a v a 2s .c om*/ private static Map<String, Model> getReactorModels(String path, Model model, MavenProject project, Log logger) throws IOException { if (path.length() > 0 && !path.endsWith("/")) { path += '/'; } Map<String, Model> result = new LinkedHashMap<String, Model>(); Map<String, Model> childResults = new LinkedHashMap<String, Model>(); File baseDir = path.length() > 0 ? new File(project.getBasedir(), path) : project.getBasedir(); Set<String> childModules = getAllChildModules(model, logger); removeMissingChildModules(logger, baseDir, childModules); for (String moduleName : childModules) { String modulePath = path + moduleName; File moduleDir = new File(baseDir, moduleName); File moduleProjectFile; if (moduleDir.isDirectory()) { moduleProjectFile = new File(moduleDir, "pom.xml"); } else { // i don't think this should ever happen... but just in case // the module references the file-name moduleProjectFile = moduleDir; } try { // the aim of this goal is to fix problems when the project cannot be parsed by Maven // so we have to work with the raw model and not the interpolated parsed model from maven Model moduleModel = getRawModel(moduleProjectFile); result.put(modulePath, moduleModel); childResults.putAll(getReactorModels(modulePath, moduleModel, project, logger)); } catch (IOException e) { logger.debug("Could not parse " + moduleProjectFile.getPath(), e); } } result.putAll(childResults); // more efficient update order if all children are added after siblings return result; }
From source file:org.codehaus.mojo.versions.branch.BranchMojo.java
License:Apache License
private static Map<String, ModelFileTuple> loadModels(String path, Model model, MavenProject project, Log logger) throws IOException { if (path.length() > 0 && !path.endsWith("/")) { path += '/'; }//from ww w . ja v a 2 s . c o m Map<String, ModelFileTuple> result = new LinkedHashMap<>(); Map<String, ModelFileTuple> childResults = new LinkedHashMap<>(); File baseDir = path.length() > 0 ? new File(project.getBasedir(), path) : project.getBasedir(); Set<String> childModules = PomHelper.getAllChildModules(model, logger); PomHelper.removeMissingChildModules(logger, baseDir, childModules); for (String childModuleName : childModules) { String childModulePath = path + childModuleName; File childModuleDir = new File(baseDir, childModuleName); File childModuleProjectFile; if (childModuleDir.isDirectory()) { childModuleProjectFile = new File(childModuleDir, "pom.xml"); } else { // i don't think this should ever happen... but just in case // the module references the file-name childModuleProjectFile = childModuleDir; } try { // the aim of this goal is to fix problems when the project cannot be parsed by Maven // so we have to work with the raw model and not the interpolated parsed model from maven Model childModuleModel = getRawModel(childModuleProjectFile); result.put(moduleId(childModuleModel), new ModelFileTuple(childModuleModel, childModuleProjectFile)); childResults.putAll(loadModels(childModulePath, childModuleModel, project, logger)); } catch (IOException e) { logger.debug("Could not parse " + childModuleProjectFile.getPath(), e); } } result.putAll(childResults); // more efficient update order if all children are added after siblings return result; }
From source file:org.codehaus.mojo.versions.SetMojo.java
License:Apache License
/** * Called when this mojo is executed.//from www . j ava 2 s .c o m * * @throws org.apache.maven.plugin.MojoExecutionException * when things go wrong. * @throws org.apache.maven.plugin.MojoFailureException * when things go wrong. */ public void execute() throws MojoExecutionException, MojoFailureException { if (updateMatchingVersions == null) { updateMatchingVersions = Boolean.TRUE; } if (getProject().getOriginalModel().getVersion() == null) { throw new MojoExecutionException("Project version is inherited from parent."); } if (StringUtils.isEmpty(newVersion)) { if (settings.isInteractiveMode()) { try { newVersion = prompter.prompt("Enter the new version to set", getProject().getOriginalModel().getVersion()); } catch (PrompterException e) { throw new MojoExecutionException(e.getMessage(), e); } } else { throw new MojoExecutionException("You must specify the new version, either by using the newVersion " + "property (that is -DnewVersion=... on the command line) or run in interactive mode"); } } if (StringUtils.isEmpty(newVersion)) { throw new MojoExecutionException("You must specify the new version, either by using the newVersion " + "property (that is -DnewVersion=... on the command line) or run in interactive mode"); } try { final MavenProject project = PomHelper.getLocalRoot(projectBuilder, getProject(), localRepository, null, getLog()); getLog().info("Local aggregation root: " + project.getBasedir()); Map<String, Model> reactorModels = PomHelper.getReactorModels(project, getLog()); final SortedMap<String, Model> reactor = new TreeMap<String, Model>( new ReactorDepthComparator(reactorModels)); reactor.putAll(reactorModels); // set of files to update final Set<File> files = new LinkedHashSet<File>(); getLog().info( "Processing change of " + groupId + ":" + artifactId + ":" + oldVersion + " -> " + newVersion); Pattern groupIdRegex = Pattern .compile(RegexUtils.convertWildcardsToRegex(fixNullOrEmpty(groupId, "*"), true)); Pattern artifactIdRegex = Pattern .compile(RegexUtils.convertWildcardsToRegex(fixNullOrEmpty(artifactId, "*"), true)); Pattern oldVersionIdRegex = Pattern .compile(RegexUtils.convertWildcardsToRegex(fixNullOrEmpty(oldVersion, "*"), true)); boolean found = false; for (Model m : reactor.values()) { final String mGroupId = PomHelper.getGroupId(m); final String mArtifactId = PomHelper.getArtifactId(m); final String mVersion = PomHelper.getVersion(m); if (groupIdRegex.matcher(mGroupId).matches() && artifactIdRegex.matcher(mArtifactId).matches() && oldVersionIdRegex.matcher(mVersion).matches() && !newVersion.equals(mVersion)) { found = true; // if the change is not one we have swept up already applyChange(project, reactor, files, m.getGroupId(), m.getArtifactId(), StringUtils.isBlank(oldVersion) || "*".equals(oldVersion) ? "" : m.getVersion()); } } if (!found) { applyChange(project, reactor, files, groupId, artifactId, oldVersion); } // now process all the updates for (File file : files) { process(file); } } catch (IOException e) { throw new MojoExecutionException(e.getMessage(), e); } }
From source file:org.codehaus.mojo.versions.SetMojo.java
License:Apache License
private void addFile(Set<File> files, MavenProject project, String relativePath) { final File moduleDir = new File(project.getBasedir(), relativePath); final File moduleProjectFile; if (moduleDir.isDirectory()) { moduleProjectFile = new File(moduleDir, "pom.xml"); } else {//w w w. j a v a 2s . com // i don't think this should ever happen... but just in case // the module references the file-name moduleProjectFile = moduleDir; } files.add(moduleProjectFile); }
From source file:org.codehaus.mojo.versions.SwitchMojo.java
License:Apache License
public void processAllDeps(String groupId, String artifactId, String version) throws MojoExecutionException, MojoFailureException { try {/*from w w w .ja va 2 s . c o m*/ final MavenProject project = PomHelper.getLocalRoot(projectBuilder, getProject(), localRepository, null, getLog()); getLog().info("Process All Modules Local aggregation root: " + project.getBasedir()); final Map<String, Model> reactor = PomHelper.getReactorModels(project, getLog()); final List<String> order = new ArrayList<String>(reactor.keySet()); Collections.sort(order, new ReactorDepthComparator(reactor)); final Iterator i = order.iterator(); while (i.hasNext()) { final String sourcePath = (String) i.next(); final Model sourceModel = (Model) reactor.get(sourcePath); getLog().debug(sourcePath.length() == 0 ? "Process All Modules Processing root module as parent" : "Processing " + sourcePath + " as a parent."); final String sourceGroupId = PomHelper.getGroupId(sourceModel); if (sourceGroupId == null) { getLog().warn("Module " + sourcePath + " is missing a groupId."); continue; } final String sourceArtifactId = PomHelper.getArtifactId(sourceModel); if (sourceArtifactId == null) { getLog().warn("Module " + sourcePath + " is missing an artifactId."); continue; } final String sourceVersion = PomHelper.getVersion(sourceModel); if (sourceVersion == null) { getLog().warn("Module " + sourcePath + " is missing a version."); continue; } if (PomHelper.isExplicitVersion(sourceModel)) { List<Dependency> dd = sourceModel.getDependencies(); for (Dependency d : dd) { if (d.getArtifactId().equals(artifactId) && d.getGroupId().equals(groupId) && d.getVersion().equals(version)) { String newVersion2 = incrementModuleVersion(PomHelper.getGroupId(sourceModel), PomHelper.getArtifactId(sourceModel), PomHelper.getVersion(sourceModel)); if (!newVersion2.equals(sourceModel.getVersion())) { updateModuleVersion(PomHelper.getGroupId(sourceModel), PomHelper.getArtifactId(sourceModel), PomHelper.getVersion(sourceModel), newVersion2); } } } } } } catch (IOException e) { throw new MojoExecutionException(e.getMessage(), e); } }
From source file:org.codehaus.mojo.versions.SwitchMojo.java
License:Apache License
public void processAllModules() throws MojoExecutionException, MojoFailureException { try {/*from w ww . j a v a 2s . c o m*/ final MavenProject project = PomHelper.getLocalRoot(projectBuilder, getProject(), localRepository, null, getLog()); getLog().info("Process All Modules Local aggregation root: " + project.getBasedir()); final Map<String, Model> reactor = PomHelper.getReactorModels(project, getLog()); final List<String> order = new ArrayList<String>(reactor.keySet()); Collections.sort(order, new ReactorDepthComparator(reactor)); final Iterator i = order.iterator(); while (i.hasNext()) { final String sourcePath = (String) i.next(); final Model sourceModel = (Model) reactor.get(sourcePath); getLog().debug(sourcePath.length() == 0 ? "Process All Modules Processing root module as parent" : "Processing " + sourcePath + " as a parent."); final String sourceGroupId = PomHelper.getGroupId(sourceModel); if (sourceGroupId == null) { getLog().warn("Module " + sourcePath + " is missing a groupId."); continue; } final String sourceArtifactId = PomHelper.getArtifactId(sourceModel); if (sourceArtifactId == null) { getLog().warn("Module " + sourcePath + " is missing an artifactId."); continue; } final String sourceVersion = PomHelper.getVersion(sourceModel); if (sourceVersion == null) { getLog().warn("Module " + sourcePath + " is missing a version."); continue; } String newVersion = updateSuffix(PomHelper.getGroupId(sourceModel), PomHelper.getArtifactId(sourceModel), PomHelper.getVersion(sourceModel)); updateModuleVersion(PomHelper.getGroupId(sourceModel), PomHelper.getArtifactId(sourceModel), PomHelper.getVersion(sourceModel), newVersion); } } catch (IOException e) { throw new MojoExecutionException(e.getMessage(), e); } }
From source file:org.codehaus.mojo.versions.SwitchMojo.java
License:Apache License
public void updateModuleVersion(String groupId, String artifactId, String version, String newVersion) throws MojoExecutionException, MojoFailureException { // this is the triggering change addChange(groupId, artifactId, version, newVersion); try {/* w ww . j av a 2 s . co m*/ final MavenProject project = PomHelper.getLocalRoot(projectBuilder, getProject(), localRepository, null, getLog()); getLog().info("Local aggregation root: " + project.getBasedir()); final Map<String, Model> reactor = PomHelper.getReactorModels(project, getLog()); // now fake out the triggering change final Model current = PomHelper.getModel(reactor, getProject().getGroupId(), getProject().getArtifactId()); current.setVersion(newVersion); files.add(getProject().getFile()); final List<String> order = new ArrayList<String>(reactor.keySet()); Collections.sort(order, new ReactorDepthComparator(reactor)); final Iterator i = order.iterator(); while (i.hasNext()) { final String sourcePath = (String) i.next(); final Model sourceModel = (Model) reactor.get(sourcePath); getLog().debug(sourcePath.length() == 0 ? "Processing root module as parent" : "Processing " + sourcePath + " as a parent."); final String sourceGroupId = PomHelper.getGroupId(sourceModel); if (sourceGroupId == null) { getLog().warn("Module " + sourcePath + " is missing a groupId."); continue; } final String sourceArtifactId = PomHelper.getArtifactId(sourceModel); if (sourceArtifactId == null) { getLog().warn("Module " + sourcePath + " is missing an artifactId."); continue; } final String sourceVersion = PomHelper.getVersion(sourceModel); if (sourceVersion == null) { getLog().warn("Module " + sourcePath + " is missing a version."); continue; } final File moduleDir = new File(project.getBasedir(), sourcePath); final File moduleProjectFile; if (moduleDir.isDirectory()) { moduleProjectFile = new File(moduleDir, "pom.xml"); } else { // i don't think this should ever happen... but just in case // the module references the file-name moduleProjectFile = moduleDir; } files.add(moduleProjectFile); getLog().debug("Looking for modules which use " + ArtifactUtils.versionlessKey(sourceGroupId, sourceArtifactId) + " as their parent"); final Iterator j = PomHelper.getChildModels(reactor, sourceGroupId, sourceArtifactId).entrySet() .iterator(); while (j.hasNext()) { final Map.Entry target = (Map.Entry) j.next(); final String targetPath = (String) target.getKey(); final Model targetModel = (Model) target.getValue(); final Parent parent = targetModel.getParent(); getLog().debug("Module: " + targetPath); if (sourceVersion.equals(parent.getVersion())) { getLog().debug(" parent already is " + ArtifactUtils.versionlessKey(sourceGroupId, sourceArtifactId) + ":" + sourceVersion); } else { getLog().debug( " parent is " + ArtifactUtils.versionlessKey(sourceGroupId, sourceArtifactId) + ":" + parent.getVersion()); getLog().debug( " will become " + ArtifactUtils.versionlessKey(sourceGroupId, sourceArtifactId) + ":" + sourceVersion); } final boolean targetExplicit = PomHelper.isExplicitVersion(targetModel); Boolean updateMatchingVersions = true; if ((updateMatchingVersions.booleanValue() || !targetExplicit) && StringUtils.equals(parent.getVersion(), PomHelper.getVersion(targetModel))) { getLog().debug(" module is " + ArtifactUtils.versionlessKey(PomHelper.getGroupId(targetModel), PomHelper.getArtifactId(targetModel)) + ":" + PomHelper.getVersion(targetModel)); getLog().debug( " will become " + ArtifactUtils.versionlessKey(PomHelper.getGroupId(targetModel), PomHelper.getArtifactId(targetModel)) + ":" + sourceVersion); addChange(PomHelper.getGroupId(targetModel), PomHelper.getArtifactId(targetModel), PomHelper.getVersion(targetModel), sourceVersion); targetModel.setVersion(sourceVersion); } else { getLog().debug(" module is " + ArtifactUtils.versionlessKey(PomHelper.getGroupId(targetModel), PomHelper.getArtifactId(targetModel)) + ":" + PomHelper.getVersion(targetModel)); } } } } catch (IOException e) { throw new MojoExecutionException(e.getMessage(), e); } }
From source file:org.commonjava.maven.plugins.execroot.DirectoryOfGoal.java
License:Apache License
/** * {@inheritDoc}/*from w w w . j a v a2 s .co m*/ * @throws MojoExecutionException * @see org.commonjava.maven.plugins.execroot.AbstractDirectoryGoal#findDirectory() */ @Override protected File findDirectory() throws MojoExecutionException { File dir = null; final Stack<MavenProject> toCheck = new Stack<MavenProject>(); toCheck.addAll(projects); while (!toCheck.isEmpty()) { final MavenProject p = toCheck.pop(); if (project.matches(p)) { dir = p.getBasedir(); break; } if (p.getParent() != null) { toCheck.add(p.getParent()); } } if (dir == null) { throw new MojoExecutionException("Cannot find directory for project: " + project); } return dir; }
From source file:org.commonjava.maven.plugins.execroot.HighestBasedirGoal.java
License:Apache License
/** * {@inheritDoc}/*from w w w.j av a 2s . co m*/ * @throws MojoExecutionException * @see org.commonjava.maven.plugins.execroot.AbstractDirectoryGoal#findDirectory() */ @Override protected File findDirectory() throws MojoExecutionException { final Stack<MavenProject> toCheck = new Stack<MavenProject>(); toCheck.addAll(projects); final List<File> files = new ArrayList<File>(); while (!toCheck.isEmpty()) { final MavenProject p = toCheck.pop(); if (p.getBasedir() == null) { // we've hit a parent that was resolved. Don't bother going higher up the hierarchy. continue; } if (!files.contains(p.getBasedir())) { // add to zero to maybe help pre-sort the paths...the shortest (parent) paths should end up near the // top. files.add(0, p.getBasedir()); } if (p.getParent() != null) { toCheck.add(p.getParent()); } } if (files.isEmpty()) { throw new MojoExecutionException("No project base directories found! Are you sure you're " + "executing this on a valid Maven project?"); } Collections.sort(files, new PathComparator()); final File dir = files.get(0); if (files.size() > 1) { final File next = files.get(1); if (!next.getAbsolutePath().startsWith(dir.getAbsolutePath())) { throw new MojoExecutionException("Cannot find a single highest directory for this project set. " + "First two candidates directories don't share a common root."); } } return dir; }