Java org.sonatype.plexus.build.incremental.BuildContext 代码实例

・25 分钟阅读

以下是展示如何使用org.sonatype.plexus.build.incremental.BuildContext的最佳示例。 我们使用了代码质量辨别算法从开源项目中提取出了最佳的优秀示例。

实例 1


@Override
public Set<IProject> build( int kind, IProgressMonitor monitor )
    throws Exception
{
    IMaven maven = MavenPlugin.getMaven();
    BuildContext buildContext = getBuildContext();
    // check if any of the grammar files changed
    File source = maven.getMojoParameterValue(getSession(), getMojoExecution(), "protoSourceRoot", File.class);
    Scanner ds = buildContext.newScanner( source ); // delta or full scanner
    ds.scan();
    String[] includedFiles = ds.getIncludedFiles();
    if (includedFiles == null || includedFiles.length <= 0 )
    {
        return null;
    }
    // execute mojo
    Set<IProject> result = super.build( kind, monitor );
    // tell m2e builder to refresh generated files
    File generated = maven.getMojoParameterValue(getSession(), getMojoExecution(), "outputDirectory", File.class);
    if (generated != null) {
        buildContext.refresh( generated );
    }
    return result;
}
 

实例 2


@Override
public Set<IProject> build(int kind, IProgressMonitor monitor)
                throws Exception {
        // execute mojo
        Set<IProject> result = super.build(kind, monitor);
        IMaven maven = MavenPlugin.getMaven();
        BuildContext buildContext = getBuildContext();
        MojoExecution mojoExecution = getMojoExecution();
        File targetDirectory = maven.getMojoParameterValue(getSession(),
                        getMojoExecution(), "outputDirectory", File.class);
        if (targetDirectory != null) {
                buildContext.refresh(targetDirectory);
        }
        return result;
}
 

实例 3


/**
 * {@inheritDoc}
 * 
 * @see org.eclipse.m2e.core.project.configurator.MojoExecutionBuildParticipant#build(int,
 *      org.eclipse.core.runtime.IProgressMonitor)
 */
@Override
public Set<IProject> build(int kind, IProgressMonitor monitor)
                throws Exception {
        final IMaven maven = MavenPlugin.getMaven();
        final BuildContext buildContext = getBuildContext();
        final MavenProject project = getMavenProjectFacade().getMavenProject();
        final MojoExecution mojoExecution = getMojoExecution();
        // execute mojo
        final Set<IProject> result = super.build(kind, monitor);
        // tell m2e builder to refresh generated files
        File generated = maven.getMojoParameterValue(project, mojoExecution,
                        "outputDirectory", File.class, monitor);
        if (generated != null) {
                buildContext.refresh(generated);
        }
        return result;
}
 

实例 4


@Override
public Set<IProject> build( int kind, IProgressMonitor monitor )
    throws Exception
{
    IMaven maven = MavenPlugin.getMaven();
    BuildContext buildContext = getBuildContext();
    // execute mojo
    Set<IProject> result = super.build( kind, monitor );
    // tell m2e builder to refresh generated files
    File generated = maven.getMojoParameterValue(getSession(), getMojoExecution(), "sourceRoot", File.class);
    if (generated != null) {
        buildContext.refresh( generated );
    }
    return result;
}
 

实例 5


/**
 * Delete a directory and its content.
 * 
 * @param dir
 * @throws IOException
 * @since 3.3
 */
public final void dirRemove(File dir) throws IOException {
        if (dir!=null) {
                getLog().debug("Deleting tree: "+dir.toString()); //$NON-NLS-1$
                LinkedList<File> candidates = new LinkedList<File>();
                candidates.add(dir);
                File f;
                File[] children;
                BuildContext buildContext = getBuildContext();
                while (!candidates.isEmpty()) {
                        f = candidates.getFirst();
                        getLog().debug("Scanning: "+f); //$NON-NLS-1$
                        if (f.isDirectory()) {
                                children = f.listFiles();
                                if (children!=null && children.length>0) {
                                        // Non empty directory
                                        for(File c : children) {
                                                getLog().debug("Discovering: "+c); //$NON-NLS-1$
                                                candidates.push(c);
                                        }
                                }
                                else {
                                        // empty directory
                                        getLog().debug("Deleting: "+f); //$NON-NLS-1$
                                        candidates.removeFirst();
                                        f.delete();
                                        buildContext.refresh(f.getParentFile());
                                }
                        }
                        else {
                                // not a directory
                                candidates.removeFirst();
                                if (f.exists()) {
                                        getLog().debug("Deleting: "+f); //$NON-NLS-1$
                                        f.delete();
                                        buildContext.refresh(f.getParentFile());
                                }
                        }
                }
                getLog().debug("Deletion done"); //$NON-NLS-1$
        }
}
 

实例 6


private void refreshOutputDirectory(final BuildContext buildContext, final File outputDirectory) {
                if (outputDirectory != null && outputDirectory.exists()) {
                        buildContext.refresh(outputDirectory);
                }
        }
 

实例 7


@Override
public Set<IProject> build(int kind, IProgressMonitor monitor) throws Exception {
        final IMaven maven = MavenPlugin.getMaven();
        final BuildContext buildContext = getBuildContext();
        final MavenProject project = getMavenProjectFacade().getMavenProject();
        final MojoExecution mojoExecution = getMojoExecution();
        // check if initial generation has been done
        File stale = maven.getMojoParameterValue(project, mojoExecution, "staleFile", File.class, monitor);
        if (stale != null && stale.exists()) {
                // check if any of the binding files have changed
                File source = maven.getMojoParameterValue(project, mojoExecution, "bindingDirectory", File.class, monitor);
                Scanner ds = buildContext.newScanner(source); // delta or full
                                                                                                                // scanner
                try {
                        ds.scan();
                        String[] includedBindingFiles = ds.getIncludedFiles();
                        // check if any of the wsdl files have changed
                        source = maven.getMojoParameterValue(project, mojoExecution, "wsdlDirectory", File.class, monitor);
                        ds = buildContext.newScanner(source); // delta or full
                                                                                                        // scanner
                        ds.scan();
                        String[] includedWsdlFiles = ds.getIncludedFiles();
                        // If no changes, simply return
                        if ((includedBindingFiles == null || includedBindingFiles.length <= 0)
                                        && (includedWsdlFiles == null || includedWsdlFiles.length <= 0)) {
                                return null;
                        }
                }
                catch (NullPointerException e) {
                        e.printStackTrace();
                }
        }
        // Otherwise, execute mojo
        final Set<IProject> result = super.build(kind, monitor);
        // tell m2e builder to refresh generated files
        File generated = maven.getMojoParameterValue(project, mojoExecution, "sourceDestDir", File.class, monitor);
        if (generated != null) {
                buildContext.refresh(generated);
        }
        return result;
}
 

实例 8


@Override
public Set<IProject> build(int kind, IProgressMonitor monitor)
    throws Exception {
  IMavenProjectFacade mavenProject = getMavenProjectFacade();
  IPath[] paths;
  BuildContext context = getBuildContext();
  String goal = getMojoExecution().getGoal();
  if ("generateAsync".equals(goal)) {
    paths = mavenProject.getCompileSourceLocations();
  } else {
    paths = mavenProject.getResourceLocations();
  }
  /*
   * look at source or resource folders to see if a file has been modified.
   * In the positive case, launch the mojo and refresh the generation
   * directory. In the negative, don't do anything.
   */
  //TODO: add some filters based on the mojo and mojo configuration
  boolean act = false;
  for(int i = 0; !act && i < paths.length;++i) {
    Scanner scanner = context.newScanner(mavenProject.getProject().getFolder(paths[i]).getLocation().toFile());
    scanner.scan();
    String[] includedFiles = scanner.getIncludedFiles();
    if (includedFiles != null && includedFiles.length > 0) {
      act = true;
    }
  }
  if (act) {
    Set<IProject> projects = super.build(kind, monitor);
    IMaven maven = MavenPlugin.getMaven();
    File generateDirectory = maven.getMojoParameterValue(getSession(),
        getMojoExecution(), GWTProjectConfigurator.GENERATE_DIRECTORY_PARAMETER_NAME, File.class);
    if (null != generateDirectory) {
      context.refresh(generateDirectory);
    }
    return projects;
  } else {
    return null;
  }
}
 

实例 9


@Override
public Set<IProject> build( final int kind, final IProgressMonitor monitor )
throws Exception
{
    IMaven maven = MavenPlugin.getMaven();
    BuildContext buildContext = getBuildContext();
    // check if any of the grammar files changed
    /*
    File source = maven.getMojoParameterValue(getSession(), getMojoExecution(), "sourceDirectory", File.class);
    Scanner ds = buildContext.newScanner( source ); // delta or full scanner
    ds.scan();
    String[] includedFiles = ds.getIncludedFiles();
    if (includedFiles == null || includedFiles.length <= 0 )
    {
        return null;
    }
     */
    // execute mojo
    log.info("About to start plugin");
    Set<IProject> result = super.build( kind, monitor );
    log.info("done.");
    // tell m2e builder to refresh generated files
    File generated = maven.getMojoParameterValue(getSession(), getMojoExecution(), "outputDirectory", File.class);
    if (generated != null) {
        buildContext.refresh( generated );
    }
    log.info("did refresh: " + generated);
    return result;
}
 

实例 10


@Override
public Set<IProject> build(int kind, IProgressMonitor monitor) throws Exception {
    final IMaven maven = MavenPlugin.getMaven();
    final BuildContext buildContext = getBuildContext();
    final MojoExecution mojoExecution = getMojoExecution();
    final Set<IProject> result = super.build(kind, monitor);
    final File generated =
                    maven.getMojoParameterValue(getMavenProjectFacade().getMavenProject(), mojoExecution, "generateDirectory", File.class, monitor);
    if (generated != null)
    {
        buildContext.refresh(generated);
    }
    return result;
}
 

实例 11


private static boolean sourcesChanged(BuildContext buildContext,
                File[] sourceLocations) {
        for (File sourceLocation : sourceLocations) {
                if (sourceLocation.isFile()) {
                        if (buildContext.hasDelta(sourceLocation)) {
                                return true;
                        }
                } else {
                        // delta or full scanner
                        Scanner ds = buildContext.newScanner(sourceLocation);
                        ds.setIncludes(JFLEX_INCLUDES);
                        ds.scan();
                        String[] includedFiles = ds.getIncludedFiles();
                        if (includedFiles != null && includedFiles.length > 0) {
                                return true;
                        }
                }
        }
        return false;
}
 

实例 12


@Override
public Set<IProject> build(int kind, IProgressMonitor monitor)
                throws Exception {
        IMaven maven = MavenPlugin.getMaven();
        BuildContext buildContext = getBuildContext();
        MojoExecution mojoExecution = getMojoExecution();
        File source = maven.getMojoParameterValue(getSession(),
                        mojoExecution, "sourceDirectory", File.class);
        if(!source.exists() || !source.isDirectory()) {
                // skip non-existent source directories
                return null;
        }
        // check if any of the input files changed
        Scanner ds = buildContext.newScanner(source); // delta or full scanner
        ds.scan();
        String[] includedFiles = ds.getIncludedFiles();
        if (includedFiles == null || includedFiles.length <= 0) {
                return null;
        }
        // override outputDirectory
        File outputDirectory = maven.getMojoParameterValue(getSession(),
                        mojoExecution, "outputDirectory", File.class);
        File alternateOutputDirectory = new File(
                        outputDirectory.getParentFile(), "js-"
                                        + outputDirectory.getName());
        Xpp3Dom dom = mojoExecution.getConfiguration();
        Xpp3Dom outputDirectoryConfig = dom.getChild("outputDirectory");
        outputDirectoryConfig.setValue(alternateOutputDirectory
                        .getAbsolutePath());
        // execute mojo
        Set<IProject> result = super.build(kind, monitor);
        // tell m2e builder to refresh generated files
        buildContext.refresh(alternateOutputDirectory);
        return result;
}
 

实例 13


@Override
public Set<IProject> build( int kind, IProgressMonitor monitor )
    throws Exception
{
    IMaven maven = MavenPlugin.getMaven();
    BuildContext buildContext = getBuildContext();
    // check if any of the grammar files changed
    File source = maven.getMojoParameterValue(getSession(), getMojoExecution(), "sourceDirectory", File.class);
    Scanner ds = buildContext.newScanner( source ); // delta or full scanner
    ds.scan();
    String[] includedFiles = ds.getIncludedFiles();
    if (includedFiles == null || includedFiles.length <= 0 )
    {
        return null;
    }
    // execute mojo
    Set<IProject> result = super.build( kind, monitor );
    // tell m2e builder to refresh generated files
    File generated = maven.getMojoParameterValue(getSession(), getMojoExecution(), "outputDirectory", File.class);
    if (generated != null) {
        buildContext.refresh( generated );
    }
    return result;
}
 

实例 14


@Override
public Set<IProject> build(int kind, IProgressMonitor monitor) throws Exception {
        IMaven maven = MavenPlugin.getMaven();
        BuildContext buildContext = getBuildContext();
        monitor.setTaskName("Executing " + getMojoExecution().getArtifactId()+ ":" +getMojoExecution().getGoal());
        //Modifying the pom triggers a build, otherwise, check for java source modifications
        if (!buildContext.hasDelta(getMavenProjectFacade().getPomFile())) {
          // check if any of the java files changed
          File source = maven.getMojoParameterValue(getSession(), getMojoExecution(), MavenProcessorExecutionDelegate.SOURCE_DIRECTORY_PARAMETER, File.class);
          Scanner ds = buildContext.newScanner(source); // delta or full scanner
          ds.scan();
          String[] includedFiles = ds.getIncludedFiles();
          if (includedFiles == null || includedFiles.length <= 0) {
            return null;
          }
          if (getBuildContext().isIncremental()) {
            boolean interestingFileChanged = false;
            for (String f : includedFiles) {
              if (f.endsWith(".java")) {
                interestingFileChanged = true;
                break;
              }
            }
            if (!interestingFileChanged) {
              return Collections.emptySet();
            }
          }
        }
        // execute mojo
        Set<IProject> result = super.build(kind, monitor);
        // tell m2e builder to refresh generated files
        File generated = maven.getMojoParameterValue(getSession(), getMojoExecution(), MavenProcessorExecutionDelegate.OUTPUT_DIRECTORY_PARAMETER, File.class);
        if (generated == null) {
                generated = maven.getMojoParameterValue(getSession(), getMojoExecution(), MavenProcessorExecutionDelegate.DEFAULT_OUTPUT_DIRECTORY_PARAMETER, File.class);
        }
        if (generated != null) {
                buildContext.refresh(generated);
        }
        return result;
}
 

实例 15


@Override
public Set<IProject> build(int kind, IProgressMonitor monitor)
                throws Exception {
        final IMaven maven = MavenPlugin.getMaven();
        final BuildContext buildContext = getBuildContext();
        final MavenSession mavenSession = getSession();
        final MojoExecution mojoExecution = getMojoExecution();
        File sourceRoot = maven.getMojoParameterValue(mavenSession,
                        mojoExecution, "sourceRoot", File.class);
        boolean filesModified = false;
        if (sourceRoot != null && !sourceRoot.exists()) {
                filesModified = true;
        } else {
                final List wsdlOptions = maven.getMojoParameterValue(mavenSession, mojoExecution, "wsdlOptions", List.class);
                // getMojoParameterValue returns an instance of WsdlOption from a different classloader, so casting doesn't work.
                for (Object obj : wsdlOptions) {
                        Class k = obj.getClass();
                        Method getWsdl = k.getMethod("getWsdl");
                        Method getBindingFiles = k.getMethod("getBindingFiles");
                        String wsdl = getWsdl.invoke(obj).toString();
                        filesModified = (!StringUtils.isEmpty(wsdl) && !ArrayUtils
                                        .isEmpty(BuildHelper.getModifiedFiles(buildContext,
                                                        new File(wsdl))));
                        if (!filesModified) {
                                String[] bindingFiles = (String[])getBindingFiles.invoke(obj);
                                for (String bindingFile : bindingFiles) {
                                        filesModified = (!StringUtils.isEmpty(bindingFile) && !ArrayUtils
                                                        .isEmpty(BuildHelper.getModifiedFiles(
                                                                        buildContext, new File(bindingFile))));
                                        if (filesModified) {
                                                break;
                                        }
                                }
                        }
                        if (filesModified) {
                                break;
                        }
                }
        }
        if (!filesModified) {
                return null;
        }
        final Set<IProject> result = super.build(kind, monitor);
        if (sourceRoot != null) {
                buildContext.refresh(sourceRoot);
        }
        return result;
}
 
讨论
淘淘あ西西 profile image