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

The following examples show how to use org.apache.maven.model.Model#getBuild() . These examples are extracted from open source projects. 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 Project: helidon-build-tools   File: PomUtils.java    License: 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
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 3
Source Project: repairnator   File: ComputePlugins.java    License: 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 4
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 5
/**
 * 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 6
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 7
Source Project: butterfly   File: AbstractArtifactPomOperation.java    License: 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 8
Source Project: butterfly   File: AbstractArtifactPomOperation.java    License: 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 9
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 10
Source Project: scava   File: BuildManager.java    License: 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 11
@Override
public PluginManagement get( Model model )
{
    if ( model.getBuild() == null )
    {
        return null;
    }
    return model.getBuild().getPluginManagement();
}
 
Example 12
@Override
public void set( Model model, PluginManagement value )
{
    if ( model.getBuild() == null )
    {
        model.setBuild( new Build() );
    }
    model.getBuild().setPluginManagement( value );
}
 
Example 13
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 14
Source Project: furnace   File: ProjectHelper.java    License: 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 15
Source Project: butterfly   File: AbstractArtifactPomOperation.java    License: 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);
}
 
Example 16
Source Project: netbeans   File: OSGILookupProvider.java    License: 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 17
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 18
@Override
public Build get( Model model )
{
    Build result = model.getBuild();
    return result;
}
 
Example 19
Source Project: developer-studio   File: MavenUtils.java    License: 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 20
/**
 * 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;
}