@Override
  public boolean updateModel(Model model) throws MavenExecutionException {
    Map<String, String> versionOverrides = getVersionOverrides();
    if (versionOverrides.size() == 0) {
      return false;
    }

    // If the model doesn't have any plugin management set by default, create one for it
    PluginManagement pluginManagement = model.getBuild().getPluginManagement();
    if (pluginManagement == null) {
      pluginManagement = new PluginManagement();
      model.getBuild().setPluginManagement(pluginManagement);
      Log.getLog().debug("Created new Plugin Management for model");
    }

    // Override plugin management versions
    applyOverrides(pluginManagement.getPlugins(), versionOverrides);

    // Override plugin versions
    List<Plugin> projectPlugins = model.getBuild().getPlugins();
    applyOverrides(projectPlugins, versionOverrides);

    // Include the overrides in the built files for repeatability
    writeOverrideMap(model, getName(), versionOverrides);

    // Assuming the Model changed since overrides were given
    return true;
  }
Esempio n. 2
0
 @Override
 public void addPlugin(MavenPlugin plugin) {
   MavenCoreFacet mavenCoreFacet = project.getFacet(MavenCoreFacet.class);
   Model pom = mavenCoreFacet.getPOM();
   pom.getBuild().addPlugin(new MavenPluginAdapter(plugin));
   mavenCoreFacet.setPOM(pom);
 }
  @Test
  public void testTPCNotExists() throws Exception {
    final Model model = new Model();
    final List<Model> hierarchy = asList(model);

    tpcAssembler.assembleTPCInheritance(hierarchy);

    assertSame(model, hierarchy.get(0));
    assertNull(model.getBuild());
  }
  @Override
  public Model getLifecycleModel(final Model model) {
    if (model == null) {
      throw new NullPointerException("model");
    }

    final Model lifecycleModel = new Model();
    lifecycleModel.setBuild(new Build());
    lifecycleModel
        .getBuild()
        .setPluginManagement(
            model.getBuild() != null ? model.getBuild().getPluginManagement() : null);

    lifecycleModel
        .getBuild()
        .getPlugins()
        .addAll(this.getPluginsBoundByDefaultToAllLifecycles(model.getPackaging()));

    return lifecycleModel;
  }
Esempio n. 5
0
 @Override
 public void removePlugin(Dependency dependency) {
   MavenCoreFacet mavenCoreFacet = project.getFacet(MavenCoreFacet.class);
   List<Plugin> pomPlugins = mavenCoreFacet.getPOM().getBuild().getPlugins();
   for (Plugin pomPlugin : pomPlugins) {
     if (pomPlugin.getGroupId().equals(dependency.getGroupId())
         && pomPlugin.getArtifactId().equals(dependency.getArtifactId())) {
       Model pom = mavenCoreFacet.getPOM();
       pom.getBuild().removePlugin(pomPlugin);
       mavenCoreFacet.setPOM(pom);
     }
   }
 }
 /** {@inheritDoc} */
 @Override
 public MavenPomDescriptor scan(Model model, String path, Scope scope, Scanner scanner)
     throws IOException {
   MavenPomDescriptor pomDescriptor = createMavenPomDescriptor(model, scanner);
   ScannerContext scannerContext = scanner.getContext();
   Store store = scannerContext.getStore();
   addParent(pomDescriptor, model, scannerContext);
   addProfiles(pomDescriptor, model, scannerContext);
   addProperties(pomDescriptor, model.getProperties(), store);
   addModules(pomDescriptor, model.getModules(), store);
   addManagedDependencies(
       pomDescriptor,
       model.getDependencyManagement(),
       scannerContext,
       PomManagesDependencyDescriptor.class);
   addDependencies(
       pomDescriptor, model.getDependencies(), PomDependsOnDescriptor.class, scannerContext);
   addManagedPlugins(pomDescriptor, model.getBuild(), scannerContext);
   addPlugins(pomDescriptor, model.getBuild(), scannerContext);
   addLicenses(pomDescriptor, model, store);
   return pomDescriptor;
 }
  @Override
  public boolean install() {
    if (!this.isInstalled()) {
      for (DirectoryResource folder : this.getSourceFolders()) {
        folder.mkdirs();
      }

      // FIXME WOW this needs to be simplified somehow...
      MavenCoreFacet maven = project.getFacet(MavenCoreFacet.class);
      Model pom = maven.getPOM();
      Build build = pom.getBuild();
      if (build == null) {
        build = new Build();
      }
      List<Plugin> plugins = build.getPlugins();
      Plugin javaSourcePlugin = null;
      for (Plugin plugin : plugins) {
        if ("org.apache.maven.plugins".equals(plugin.getGroupId())
            && "maven-compiler-plugin".equals(plugin.getArtifactId())) {
          javaSourcePlugin = plugin;
        }
      }

      if (javaSourcePlugin == null) {
        javaSourcePlugin = new Plugin();
        // FIXME this should find the most recent version using DependencyResolver
        javaSourcePlugin.setGroupId("org.apache.maven.plugins");
        javaSourcePlugin.setArtifactId("maven-compiler-plugin");
        javaSourcePlugin.setVersion("2.3.2");

        try {
          Xpp3Dom dom =
              Xpp3DomBuilder.build(
                  new ByteArrayInputStream(
                      "<configuration><source>1.6</source><target>1.6</target></configuration>"
                          .getBytes()),
                  "UTF-8");

          javaSourcePlugin.setConfiguration(dom);
        } catch (Exception e) {
          throw new ProjectModelException(e);
        }
      }

      build.addPlugin(javaSourcePlugin);
      pom.setBuild(build);
      maven.setPOM(pom);
    }
    return true;
  }
  @Command
  public void custom(
      PipeOut out,
      @Option(name = "artifact", required = true) String artifact,
      @Option(name = "group", required = true) String group,
      @Option(name = "version", required = true) String version) {
    final MavenCoreFacet mvnFacet = project.getFacet(MavenCoreFacet.class);

    Model pom = mvnFacet.getPOM();

    org.apache.maven.model.Plugin plugin = new org.apache.maven.model.Plugin();

    plugin.setGroupId(group);
    plugin.setArtifactId(artifact);
    plugin.setVersion(version);

    if (pom.getBuild().getPlugins().contains(plugin)) {
      return;
    }

    pom.getBuild().getPlugins().add(plugin);

    mvnFacet.setPOM(pom);
  }
 static InputLocation findLocationForManagedArtifact(
     final ManagedArtifactRegion region, MavenProject mavprj) {
   Model mdl = mavprj.getModel();
   InputLocation openLocation = null;
   if (region.isDependency) {
     DependencyManagement dm = mdl.getDependencyManagement();
     if (dm != null) {
       List<Dependency> list = dm.getDependencies();
       String id = region.groupId + ":" + region.artifactId + ":"; // $NON-NLS-1$ //$NON-NLS-2$
       if (list != null) {
         for (Dependency dep : list) {
           if (dep.getManagementKey().startsWith(id)) {
             InputLocation location = dep.getLocation(ARTIFACT_ID); // $NON-NLS-1$
             // when would this be null?
             if (location != null) {
               openLocation = location;
               break;
             }
           }
         }
       }
     }
   }
   if (region.isPlugin) {
     Build build = mdl.getBuild();
     if (build != null) {
       PluginManagement pm = build.getPluginManagement();
       if (pm != null) {
         List<Plugin> list = pm.getPlugins();
         String id = Plugin.constructKey(region.groupId, region.artifactId);
         if (list != null) {
           for (Plugin plg : list) {
             if (id.equals(plg.getKey())) {
               InputLocation location = plg.getLocation(ARTIFACT_ID); // $NON-NLS-1$
               // when would this be null?
               if (location != null) {
                 openLocation = location;
                 break;
               }
             }
           }
         }
       }
     }
   }
   return openLocation;
 }
  @Test
  public void testUseTPCFromPlugin() throws Exception {
    final List<Model> hierarchy = asList(new Model(), new Model());
    final Model pluginModel = hierarchy.get(0);

    Plugin pluginTPC = getPlugin(pluginModel, TYCHO_GROUP_ID, TYCHO_TPC_PLUGIN_ARTIFACT_ID, true);
    Xpp3Dom pluginCfg = getConfiguration(pluginTPC, true);
    addValueNode(pluginCfg, "foo", "bar");

    tpcAssembler.assembleTPCInheritance(hierarchy);

    Plugin actualPluginTPC =
        getPlugin(pluginModel, TYCHO_GROUP_ID, TYCHO_TPC_PLUGIN_ARTIFACT_ID, false);
    assertNotNull(actualPluginTPC);
    // assertSame(pluginTPC, actualPluginTPC);
    assertEquals(1, pluginModel.getBuild().getPlugins().size());
    assertPluginEquals(pluginTPC, actualPluginTPC);
  }
  public MojoProjectStub(File projectDir) {
    this.basedir = projectDir;
    props.setProperty("basedir", this.basedir.getAbsolutePath());

    File pom = new File(getBasedir(), "plugin-config.xml");
    MavenXpp3Reader pomReader = new MavenXpp3Reader();
    Model model = null;
    FileReader fileReader = null;

    try {
      fileReader = new FileReader(pom);
      model = pomReader.read(fileReader);
      setModel(model);
    } catch (Exception e) {
      throw new RuntimeException(e);
    } finally {
      IOUtils.closeQuietly(fileReader);
    }

    setGroupId(model.getGroupId());
    setArtifactId(model.getArtifactId());
    setVersion(model.getVersion());
    setName(model.getName());
    setUrl(model.getUrl());
    setPackaging(model.getPackaging());
    setFile(pom);

    build = model.getBuild();
    if (build == null) {
      build = new Build();
    }

    File srcDir = getStandardDir(getBuild().getSourceDirectory(), "src/main/java");
    getBuild().setSourceDirectory(srcDir.getAbsolutePath());

    File targetDir = getStandardDir(getBuild().getDirectory(), "target");
    getBuild().setDirectory(targetDir.getAbsolutePath());

    File outputDir = getStandardDir(getBuild().getOutputDirectory(), "target/classes");
    getBuild().setOutputDirectory(outputDir.getAbsolutePath());

    List<Resource> resources = new ArrayList<Resource>();
    resources.addAll(getBuild().getResources());

    if (resources.isEmpty()) {
      resources = new ArrayList<Resource>();
      Resource resource = new Resource();
      File resourceDir = normalize("src/main/resources");
      resource.setDirectory(resourceDir.getAbsolutePath());
      makeDirs(resourceDir);
      resources.add(resource);
    } else {
      // Make this project work on windows ;-)
      for (Resource resource : resources) {
        File dir = normalize(resource.getDirectory());
        resource.setDirectory(dir.getAbsolutePath());
      }
    }

    getBuild().setResources(resources);
  }
Esempio n. 12
0
  public void updateModel(
      Model model, String yangVersion, List<CodeGeneratorConfig> generators, String yangRoot) {
    // Model model = super.getModel();
    model.setBuild(new Build());
    Plugin plugin = new Plugin();
    plugin.setGroupId("org.opendaylight.yangtools");
    plugin.setArtifactId("yang-maven-plugin");
    plugin.setVersion(yangVersion);

    for (CodeGeneratorConfig genConf : generators) {
      Dependency dependency = new Dependency();
      dependency.setGroupId(genConf.getGroupId());
      dependency.setArtifactId(genConf.getArtifactId());
      dependency.setVersion(genConf.getVersion());
      dependency.setType("jar");
      plugin.addDependency(dependency);
    }

    PluginExecution pluginExecution = new PluginExecution();
    pluginExecution.setId("generate-sources");
    pluginExecution.addGoal("generate-sources");
    Xpp3Dom config = new Xpp3Dom("configuration");

    Xpp3Dom codeGenerators = new Xpp3Dom("codeGenerators");
    for (CodeGeneratorConfig genConf : generators) {
      Xpp3Dom generator = new Xpp3Dom("generator");
      generator.addChild(createSingleParameter("codeGeneratorClass", genConf.getGenClassName()));
      generator.addChild(createSingleParameter("outputBaseDir", genConf.getGenOutputDirectory()));
      codeGenerators.addChild(generator);
    }
    config.addChild(createSingleParameter("yangFilesRootDir", yangRoot));
    config.addChild(codeGenerators);
    config.addChild(createSingleParameter("inspectDependencies", "false"));
    pluginExecution.setConfiguration(config);

    plugin.addExecution(pluginExecution);
    model.getBuild().addPlugin(plugin);
    model.addPluginRepository(
        createRepoParameter(
            "opendaylight-release",
            "http://nexus.opendaylight.org/content/repositories/opendaylight.release/"));
    model.addPluginRepository(
        createRepoParameter(
            "opendaylight-snapshot",
            "http://nexus.opendaylight.org/content/repositories/opendaylight.snapshot/"));
    model.addRepository(
        createRepoParameter(
            "opendaylight-release",
            "http://nexus.opendaylight.org/content/repositories/opendaylight.release/"));
    model.addRepository(
        createRepoParameter(
            "opendaylight-snapshot",
            "http://nexus.opendaylight.org/content/repositories/opendaylight.snapshot/"));

    model.getProperties().put("maven.compiler.source", "1.7");
    model.getProperties().put("maven.compiler.target", "1.7");

    Dependency dependency2 = new Dependency();
    dependency2.setGroupId("org.opendaylight.yangtools");
    dependency2.setArtifactId("yang-binding");
    dependency2.setVersion(yangVersion);
    dependency2.setType("jar");
    model.addDependency(dependency2);
  }
Esempio n. 13
0
  public Model toMavenModel() {
    Model model = new Model();
    model.setBuild(new Build());
    model.setDescription(description);
    model.setUrl(url);
    model.setName(projectId.getArtifact());
    model.setGroupId(projectId.getGroup());
    model.setVersion(projectId.getVersion());
    model.setArtifactId(projectId.getArtifact());
    model.setModelVersion("4.0.0");

    // parent
    if (parent != null) {
      model.setParent(parent);
    }

    model.setPackaging(packaging);

    if (properties != null) {
      Properties modelProperties = new Properties();
      for (Property p : properties) {
        modelProperties.setProperty(p.getKey(), p.getValue());
      }
      model.setProperties(modelProperties);
    }

    // Add jar repository urls.
    if (null != repositories) {
      for (String repoUrl : repositories.getRepositories()) {
        Repository repository = new Repository();
        repository.setId(Integer.toString(repoUrl.hashCode()));
        repository.setUrl(repoUrl);
        model.addRepository(repository);
      }
    }

    // Add dependency management
    if (overrides != null) {
      DependencyManagement depMan = new DependencyManagement();
      for (Id dep : overrides) {
        Dependency dependency = new Dependency();
        dependency.setGroupId(dep.getGroup());
        dependency.setArtifactId(dep.getArtifact());
        dependency.setVersion(dep.getVersion());
        // JVZ: We need to parse these
        dependency.setType("jar");

        if (null != dep.getClassifier()) {
          dependency.setClassifier(dep.getClassifier());
        }
        depMan.addDependency(dependency);
      }
      model.setDependencyManagement(depMan);
    }

    // Add project dependencies.
    if (deps != null) {
      for (Id dep : deps) {
        Dependency dependency = new Dependency();
        dependency.setGroupId(dep.getGroup());
        dependency.setArtifactId(dep.getArtifact());
        dependency.setVersion(dep.getVersion());
        // JVZ: We need to parse these
        dependency.setType("jar");

        if (null != dep.getClassifier()) {
          dependency.setClassifier(dep.getClassifier());
        }
        model.addDependency(dependency);
      }
    }

    if (modules != null) {
      model.setModules(modules);
    }

    if (pluginOverrides != null) {
      PluginManagement management = new PluginManagement();
      management.setPlugins(pluginOverrides);
      model.getBuild().setPluginManagement(management);
    }

    if (plugins != null) {
      model.getBuild().setPlugins(plugins);
    }

    // Optional source dirs customization.
    if (dirs != null) {
      Build build = new Build();
      String srcDir = dirs.get("src");
      String testDir = dirs.get("test");

      if (null != srcDir) build.setSourceDirectory(srcDir);

      if (null != testDir) build.setTestSourceDirectory(testDir);

      model.setBuild(build);
    }

    if (null != scm) {
      Scm scm = new Scm();
      scm.setConnection(this.scm.getConnection());
      scm.setDeveloperConnection(this.scm.getDeveloperConnection());
      scm.setUrl(this.scm.getUrl());

      model.setScm(scm);
    }

    return model;
  }
  /**
   * If enabled, grab the execution root pom (which will be the topmost POM in terms of directory
   * structure). Check for the presence of the project-sources-maven-plugin in the base build
   * (/project/build/plugins/). Inject a new plugin execution for creating project sources if this
   * plugin has not already been declared in the base build section.
   */
  @Override
  public Set<Project> applyChanges(final List<Project> projects, final ManipulationSession session)
      throws ManipulationException {
    final ProjectSourcesInjectingState state = session.getState(ProjectSourcesInjectingState.class);

    // This manipulator will only run if its enabled *and* at least one other manipulator is
    // enabled.
    if (state.isEnabled() && session.anyStateEnabled(State.activeByDefault)) {
      for (final Project project : projects) {
        if (project.isExecutionRoot()) {
          logger.info("Examining {} to apply sources/metadata plugins.", project);

          final Model model = project.getModel();
          Build build = model.getBuild();
          if (build == null) {
            build = new Build();
            model.setBuild(build);
          }

          boolean changed = false;
          final Map<String, Plugin> pluginMap = build.getPluginsAsMap();
          if (state.isProjectSourcesPluginEnabled()
              && !pluginMap.containsKey(PROJECT_SOURCES_COORD)) {
            final PluginExecution execution = new PluginExecution();
            execution.setId(PROJECT_SOURCES_EXEC_ID);
            execution.setPhase(INITIALIZE_PHASE);
            execution.setGoals(Collections.singletonList(PROJECT_SOURCES_GOAL));

            final Plugin plugin = new Plugin();
            plugin.setGroupId(PROJECT_SOURCES_GID);
            plugin.setArtifactId(PROJECT_SOURCES_AID);
            plugin.setVersion(state.getProjectSourcesPluginVersion());
            plugin.addExecution(execution);

            build.addPlugin(plugin);

            changed = true;
          }

          if (state.isBuildMetadataPluginEnabled() && !pluginMap.containsKey(BMMP_COORD)) {
            final PluginExecution execution = new PluginExecution();
            execution.setId(BMMP_EXEC_ID);
            execution.setPhase(VALIDATE_PHASE);
            execution.setGoals(Collections.singletonList(BMMP_GOAL));

            final Xpp3Dom xml = new Xpp3Dom("configuration");

            final Map<String, Object> config = new HashMap<>();
            config.put("createPropertiesReport", true);
            config.put("hideCommandLineInfo", false);
            config.put("hideJavaOptsInfo", false);
            config.put("activateOutputFileMapping", true);
            config.put("addJavaRuntimeInfo", true);

            // Default name is build.properties but we currently prefer build.metadata.
            config.put("propertiesOutputFile", "build.metadata");
            // Deactivate features we don't want.
            config.put("createXmlReport", false);
            config.put("addLocallyModifiedTagToFullVersion", false);
            config.put("addToGeneratedSources", false);
            config.put("validateCheckout", false);
            config.put("forceNewProperties", true);
            config.put("addBuildDateToFullVersion", false);
            config.put("addHostInfo", false);
            config.put("addBuildDateInfo", false);
            config.put("addOsInfo", false);
            config.put("addMavenExecutionInfo", false);
            config.put("addToFilters", false);

            final Xpp3Dom additionalLocations = new Xpp3Dom("addToLocations");
            final Xpp3Dom additionalLocation = new Xpp3Dom("addToLocation");

            xml.addChild(additionalLocations);
            additionalLocations.addChild(additionalLocation);
            additionalLocation.setValue("${session.executionRootDirectory}");

            for (final Map.Entry<String, Object> entry : config.entrySet()) {
              final Xpp3Dom child = new Xpp3Dom(entry.getKey());
              if (entry.getValue() != null) {
                child.setValue(entry.getValue().toString());
              }

              xml.addChild(child);
            }

            execution.setConfiguration(xml);

            final Plugin plugin = new Plugin();
            plugin.setGroupId(BMMP_GID);
            plugin.setArtifactId(BMMP_AID);
            plugin.setVersion(state.getBuildMetadataPluginVersion());
            plugin.addExecution(execution);

            build.addPlugin(plugin);

            changed = true;
          }

          if (changed) {
            return Collections.singleton(project);
          }
        }
      }
    }

    return Collections.emptySet();
  }