Java Code Examples for org.apache.maven.model.Model#getBuild()

The following examples show how to use org.apache.maven.model.Model#getBuild() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PomUtils.java    From helidon-build-tools with Apache License 2.0 6 votes vote down vote up
private static boolean ensurePlugin(Model model) {
    org.apache.maven.model.Build build = model.getBuild();
    boolean isPresent = build.getPlugins()
                             .stream()
                             .anyMatch(p -> p.getGroupId().equals(BUILD_TOOLS_GROUP_ID)
                                     && p.getArtifactId().equals(BUILD_TOOLS_PLUGIN_ARTIFACT_ID));
    if (isPresent) {
        // Assume it is what we want rather than updating if not equal, since
        // that could undo future archetype changes.
        return false;
    } else {
        Plugin helidonPlugin = new Plugin();
        helidonPlugin.setGroupId(BUILD_TOOLS_GROUP_ID);
        helidonPlugin.setArtifactId(BUILD_TOOLS_PLUGIN_ARTIFACT_ID);
        helidonPlugin.setVersion("${" + HELIDON_PLUGIN_VERSION_PROPERTY + "}");
        helidonPlugin.setExtensions(true);
        build.addPlugin(helidonPlugin);
        return true;
    }
}
 
Example 2
Source File: PluginRemovalManipulator.java    From pom-manipulation-ext with Apache License 2.0 6 votes vote down vote up
private boolean apply( final Project project, final Model model )
{
    final PluginRemovalState state = session.getState( PluginRemovalState.class );

    if (logger.isDebugEnabled())
    {
        logger.debug( "Applying plugin changes to: {}", ga( project ) );
    }


    boolean result = false;
    List<ProjectRef> pluginsToRemove = state.getPluginRemoval();
    if ( model.getBuild() != null )
    {
        result = scanPlugins( pluginsToRemove, model.getBuild().getPlugins() );
    }

    for ( final Profile profile : ProfileUtils.getProfiles( session, model) )
    {
        if ( profile.getBuild() != null && scanPlugins( pluginsToRemove, profile.getBuild().getPlugins() ) )
        {
            result = true;
        }
    }
    return result;
}
 
Example 3
Source File: PomUpdater.java    From spring-cloud-release-tools with Apache License 2.0 6 votes vote down vote up
boolean hasSkipDeployment(Model model) {
	String property = model.getProperties().getProperty("maven.deploy.skip");
	boolean hasSkipDeploymentProperty = Boolean.parseBoolean(property);
	if (hasSkipDeploymentProperty) {
		return true;
	}
	if (model.getBuild() == null) {
		return false;
	}
	boolean plugins = model.getBuild().getPlugins().stream()
			.filter(plugin -> "maven-deploy-plugin"
					.equalsIgnoreCase(plugin.getArtifactId()))
			.map(this::skipFromConfiguration).findFirst().orElse(false);
	if (plugins) {
		return true;
	}
	if (model.getBuild().getPluginManagement() == null) {
		return false;
	}
	return model.getBuild().getPluginManagement().getPlugins().stream()
			.filter(plugin -> "maven-deploy-plugin"
					.equalsIgnoreCase(plugin.getArtifactId()))
			.map(this::skipFromConfiguration).findFirst().orElse(false);
}
 
Example 4
Source File: RequirePropertyDiverges.java    From extra-enforcer-rules with Apache License 2.0 6 votes vote down vote up
/**
 * Finds the ancestor project which defines the rule.
 *
 * @param project to inspect
 * @return the defining ancestor project.
 */
final MavenProject findDefiningParent( final MavenProject project )
{
    final Xpp3Dom invokingRule = createInvokingRuleDom();
    MavenProject parent = project;
    while ( parent != null )
    {
        final Model model = parent.getOriginalModel();
        final Build build = model.getBuild();
        if ( build != null )
        {
            final List<Xpp3Dom> rules = getRuleConfigurations( build );
            if ( isDefiningProject( rules, invokingRule ) )
            {
                break;
            }
        }
        parent = parent.getParent();
    }
    return parent;
}
 
Example 5
Source File: GitVersioningModelProcessor.java    From maven-git-versioning-extension with MIT License 6 votes vote down vote up
private void addBuildPlugin(Model model, boolean updatePomOption) {
    logger.debug(model.getArtifactId() + " temporary add build plugin");

    Plugin plugin = asPlugin();

    PluginExecution execution = new PluginExecution();
    execution.setId(GOAL);
    execution.getGoals().add(GOAL);
    plugin.getExecutions().add(execution);

    if (model.getBuild() == null) {
        model.setBuild(new Build());
    }
    model.getBuild().getPlugins().add(plugin);

    // set plugin properties
    model.getProperties().setProperty(propertyKeyPrefix + propertyKeyUpdatePom, Boolean.toString(updatePomOption));
}
 
Example 6
Source File: ComputePlugins.java    From repairnator with MIT License 6 votes vote down vote up
private List<Plugin> findPlugins(String pomPath) {
    List<File> plugins = new ArrayList<>();

    File pomFile = new File(pomPath);
    Model model = MavenHelper.readPomXml(pomFile, this.getInspector().getM2LocalPath());
    if (model == null) {
        this.addStepError("Error while building model: no model has been retrieved.");
        return null;
    }
    if (model.getBuild() == null) {
        this.addStepError("Error while obtaining build from pom.xml: build section has not been found.");
        return null;
    }
    if (model.getBuild().getPlugins() == null) {
        this.addStepError("Error while obtaining plugins from pom.xml: plugin section has not been found.");
        return null;
    }

    return model.getBuild().getPlugins();
}
 
Example 7
Source File: ProjectHelper.java    From furnace with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns <code>true</code> if this model is a single-project addon
 */
public boolean isAddon(Model model)
{
   boolean result = false;
   Build build = model.getBuild();
   if (build != null)
   {
      PLUGIN_LOOP: for (Plugin plugin : build.getPlugins())
      {
         if ("maven-jar-plugin".equals(plugin.getArtifactId()))
         {
            for (PluginExecution execution : plugin.getExecutions())
            {
               Xpp3Dom config = (Xpp3Dom) execution.getConfiguration();
               if (config != null)
               {
                  Xpp3Dom classifierNode = config.getChild("classifier");
                  if (classifierNode != null
                           && MavenAddonDependencyResolver.FORGE_ADDON_CLASSIFIER.equals(classifierNode.getValue()))
                  {
                     result = true;
                     break PLUGIN_LOOP;
                  }
               }
            }
         }
      }
   }
   return result;
}
 
Example 8
Source File: DistributionEnforcingManipulatorTest.java    From pom-manipulation-ext with Apache License 2.0 5 votes vote down vote up
private void assertSkip( final Model model, final String profileId )
{
    BuildBase build = null;
    if ( profileId != null )
    {
        final List<Profile> profiles = model.getProfiles();
        if ( profiles != null )
        {
            for ( final Profile profile : profiles )
            {
                if ( profileId.equals( profile.getId() ) )
                {
                    build = profile.getBuild();
                }
            }
        }
    }
    else
    {
        build = model.getBuild();
    }

    assertThat( build, notNullValue() );

    final Plugin plugin =
        build.getPluginsAsMap()
             .get( ga( MAVEN_PLUGIN_GROUPID, true ? MAVEN_DEPLOY_ARTIFACTID : MAVEN_INSTALL_ARTIFACTID ) );

    assertThat( plugin, notNullValue() );

    assertThat( plugin.getConfiguration()
                      .toString()
                      .contains( "<skip>" + Boolean.FALSE + "</skip>" ), equalTo( true ) );
}
 
Example 9
Source File: PomProperty.java    From flatten-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public void set( Model model, PluginManagement value )
{
    if ( model.getBuild() == null )
    {
        model.setBuild( new Build() );
    }
    model.getBuild().setPluginManagement( value );
}
 
Example 10
Source File: PomProperty.java    From flatten-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public PluginManagement get( Model model )
{
    if ( model.getBuild() == null )
    {
        return null;
    }
    return model.getBuild().getPluginManagement();
}
 
Example 11
Source File: BuildManager.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
private void rewritePOM(File pomFile) throws BuildException {
	try (Reader reader = new FileReader(pomFile)) {
		MavenXpp3Reader pomReader = new MavenXpp3Reader();
		
		Model model = pomReader.read(reader);
		reader.close();
		
		model.addRepository(createRepo("maven_central", "http://repo.maven.apache.org/maven2/", "default"));
		
		for (String id: eclipseRepos.keySet()) {
			model.addRepository(createRepo(id, eclipseRepos.get(id), "p2"));
		}
		
		Build modelBuild = model.getBuild();
		if (modelBuild == null) {
			model.setBuild(new Build());
		}
		
		model.getBuild().addPlugin(createPlugin("org.eclipse.tycho", "tycho-maven-plugin", "0.21.0", true));
		model.getBuild().addPlugin(createPlugin("org.eclipse.tycho", "target-platform-configuration", "0.21.0", false));
		model.getBuild().addPlugin(createPlugin("org.apache.maven.plugins", "maven-dependency-plugin", "2.8", false));
		
		MavenXpp3Writer pomWriter = new MavenXpp3Writer();
		pomWriter.write(new FileWriter(pomFile), model);
	} 
	catch (Exception e) {
		throw new BuildException("POM rewriting (to add plugin dependencies, cause) failed unexpectedly", e);
	}
}
 
Example 12
Source File: SurefireReportStorage.java    From smart-testing with Apache License 2.0 5 votes vote down vote up
static void copySurefireReports(Model model) {
    Build build = model.getBuild();
    if (build != null && build.getDirectory() != null) {
        File targetDir = new File(build.getDirectory());
        if (targetDir.exists() && targetDir.isDirectory()) {
            File[] surefireReports =
                targetDir.listFiles(file -> file.isDirectory() && SUREFIRE_REPORTS_DIR_NAME.equals(file.getName()));
            if (surefireReports.length > 0) {
                copyReportsDirectory(model, surefireReports[0]);
            }
        }
    }
}
 
Example 13
Source File: AbstractArtifactPomOperation.java    From butterfly with MIT License 5 votes vote down vote up
protected Plugin getManagedPlugin(Model model, String groupId, String artifactId) {
    if (model.getBuild() == null) {
        return null;
    }
    if (model.getBuild().getPluginManagement().getPlugins() == null) {
        return null;
    }
    return getPluginInList(model.getBuild().getPluginManagement().getPlugins(), groupId, artifactId);
}
 
Example 14
Source File: AbstractArtifactPomOperation.java    From butterfly with MIT License 5 votes vote down vote up
protected Plugin getPlugin(Model model, String groupId, String artifactId) {
    if (model.getBuild() == null) {
        return null;
    }
    if (model.getBuild().getPlugins() == null) {
        return null;
    }
    return getPluginInList(model.getBuild().getPlugins(), groupId, artifactId);
}
 
Example 15
Source File: GetOverviewCommand.java    From fabric8-forge with Apache License 2.0 4 votes vote down vote up
protected String findFunktionLocation(Project project, File baseDir, String action) {
    // lets try find maven stuff
    MavenFacet maven = project.getFacet(MavenFacet.class);
    List<String> directories = new ArrayList<>();
    if (maven != null) {
        Model pom = maven.getModel();
        if (pom != null) {
            Build build = pom.getBuild();
            if (build != null) {
                String sourceDirectory = build.getSourceDirectory();
                if (sourceDirectory != null) {
                    directories.add(sourceDirectory);
                }
            }
        }
    }
    directories.addAll(Arrays.asList("src/main/groovy", "src/main/kotlin", "src/main/java", "src/main/scala"));
    int idx = action.indexOf("::");
    String className = action;
    String methodName = null;
    if (idx > 0) {
        className = action.substring(0, idx);
        methodName = action.substring(idx + 2);
    }
    List<String> extensions = Arrays.asList(".groovy", ".kotlin", ".kt", ".java", "Kt.kt");
    className = className.replace('.', '/');
    for (String directory : directories) {
        File dir = new File(baseDir, directory);
        if (dir.isDirectory()) {
            // lets look for the files that match
            for (String extension : extensions) {
                String fileName = directory + "/" + className + extension;
                File file = new File(baseDir, fileName);
                if (file.isFile()) {
                    return fileName;
                }
            }
        }
    }
    return null;
}
 
Example 16
Source File: PomProperty.java    From flatten-maven-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public Build get( Model model )
{
    Build result = model.getBuild();
    return result;
}
 
Example 17
Source File: OSGILookupProvider.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void checkContent(Project prj, InstanceContent ic, AccessQueryImpl access, ForeignClassBundlerImpl bundler, RecommendedTemplates templates) {
    NbMavenProject nbprj = prj.getLookup().lookup(NbMavenProject.class);
    String effPackaging = nbprj.getPackagingType();
    
    boolean needToCheckFelixProjectTypes = true;
    if(!nbprj.isMavenProjectLoaded()) { 
        // issue #262646 
        // due to unfortunate ProjectManager.findPorjetc calls in awt, 
        // speed is essential during project init, so lets try to avoid
        // maven project loading if we can get the info faster from raw model.
        needToCheckFelixProjectTypes = false;
        Model model;
        try {
            model = nbprj.getRawModel();
        } catch (ModelBuildingException ex) {
            // whatever happend, we can't use the model, 
            // lets try to follow up with loading the maven project
            model = null;
            Logger.getLogger(OSGILookupProvider.class.getName()).log(Level.FINE, null, ex);
        }
        Build build = model != null ? model.getBuild() : null;
        List<Plugin> plugins = build != null ? build.getPlugins() : null;
        if(plugins != null) {
            for (Plugin plugin : plugins) {
                if(OSGiConstants.GROUPID_FELIX.equals(plugin.getGroupId()) && OSGiConstants.ARTIFACTID_BUNDLE_PLUGIN.equals(plugin.getArtifactId())) {
                    needToCheckFelixProjectTypes = true;
                    break;
                }
            }
        } 
    }
    if(needToCheckFelixProjectTypes) {
        String[] types = PluginPropertyUtils.getPluginPropertyList(prj, OSGiConstants.GROUPID_FELIX, OSGiConstants.ARTIFACTID_BUNDLE_PLUGIN, "supportedProjectTypes", "supportedProjectType", /*"bundle" would not work for GlassFish parent POM*/null);
        if (types != null) {
            for (String type : types) {
                if (effPackaging.equals(type)) {
                    effPackaging = NbMavenProject.TYPE_OSGI;
                }
            }
        }
    }
    if (NbMavenProject.TYPE_OSGI.equals(effPackaging)) {
        ic.add(access);
        ic.add(bundler);
        ic.add(templates);
    } else {
        ic.remove(access);
        ic.remove(bundler);
        ic.remove(templates);
    }
}
 
Example 18
Source File: MavenUtils.java    From developer-studio with Apache License 2.0 4 votes vote down vote up
private static void initializeBuildModel(MavenProject mavenProject) {
	Model model = mavenProject.getModel();
	if (model.getBuild()!=null) {
		model.setBuild(new Build());
	}
}
 
Example 19
Source File: ProjectVersionEnforcingManipulator.java    From pom-manipulation-ext with Apache License 2.0 4 votes vote down vote up
/**
 * For each project in the current build set, reset the version if using project.version
*/
@Override
public Set<Project> applyChanges( final List<Project> projects )
{
    final ProjectVersionEnforcingState state = session.getState( ProjectVersionEnforcingState.class );
    if ( !session.isEnabled() ||
                    !session.anyStateEnabled( State.activeByDefault ) ||
                    state == null || !state.isEnabled() )
    {
        logger.debug( "Project version enforcement is disabled." );
        return Collections.emptySet();
    }

    final Set<Project> changed = new HashSet<>();

    for ( final Project project : projects )
    {
        final Model model = project.getModel();

        model.getProperties().stringPropertyNames().stream().filter( k -> model.getProperties().getProperty( k ).equals(
                        Version.PROJECT_VERSION ) ).
                        forEach( k -> {
                            logger.debug( "Replacing project.version within properties for project {} with key {}", project, k );
                            model.getProperties().setProperty( k, project.getVersion() );
                            changed.add( project );
                        } );

        // TODO: We _could_ change it everywhere but it only really breaks in POM files.
        if ( model.getPackaging().equals( "pom" ) )
        {
            enforceDependencyProjectVersion( project, model.getDependencies(), changed );

            if ( model.getDependencyManagement() != null )
            {
                enforceDependencyProjectVersion( project, model.getDependencyManagement().getDependencies(), changed );
            }

            if ( model.getBuild() != null )
            {
                enforcePluginProjectVersion( project, model.getBuild().getPlugins(), changed );

                if ( model.getBuild().getPluginManagement() != null )
                {
                    enforcePluginProjectVersion( project, model.getBuild().getPluginManagement().getPlugins(), changed );
                }
            }

            final List<Profile> profiles = ProfileUtils.getProfiles( session, model);
            for ( final Profile profile : profiles )
            {
                enforceDependencyProjectVersion( project, profile.getDependencies(), changed );
                if ( profile.getDependencyManagement() != null )
                {
                    enforceDependencyProjectVersion( project, profile.getDependencyManagement().getDependencies(), changed );
                }
                if ( profile.getBuild() != null )
                {
                    enforcePluginProjectVersion( project, profile.getBuild().getPlugins(), changed );

                    if ( profile.getBuild().getPluginManagement() != null )
                    {
                        enforcePluginProjectVersion( project, profile.getBuild().getPluginManagement().getPlugins(), changed );
                    }
                }
            }
        }
    }
    if (!changed.isEmpty())
    {
        logger.warn( "Using ${project.version} in pom files may lead to unexpected errors with inheritance." );
    }
    return changed;
}
 
Example 20
Source File: AbstractArtifactPomOperation.java    From butterfly with MIT License 4 votes vote down vote up
protected Plugin getPlugin(Model model) {
    if (model.getBuild() == null) {
        return null;
    }
    return getPluginInList(model.getBuild().getPlugins(), groupId, artifactId);
}