Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012 Red Hat, Inc. and/or its affiliates.
   *
   * Licensed under the Eclipse Public License version 1.0, available at
   * http://www.eclipse.org/legal/epl-v10.html
   */
  
  package org.jboss.forge.maven.facets;
  
 import java.util.List;
 
 
 
 @Alias("forge.maven.JavaExecutionFacet")
 public class JavaExecutionFacetImpl extends BaseFacet implements JavaExecutionFacet
 {
    @Inject
 
    @Inject
    Shell shell;
 
    @Override
    public void executeProjectClass(final String fullyQualifiedClassNamefinal String... arguments)
    {
       compileProjectClasses();
 
       CommandBuilder commandBuilder = CommandBuilder.getBuilder()
                .mainClass(fullyQualifiedClassName)
                .withArguments(arguments);
 
       if (.isVerbose())
       {
          commandBuilder.setVerbose();
       }
 
       executeClass(commandBuilder.build());
    }
 
    private void executeClass(final String[] mvnArguments)
    {
       .getFacet(MavenCoreFacet.class).executeMaven(mvnArguments);
    }
 
    private void compileProjectClasses()
    {
       String[] compileArgs = { "test-compile" };
 
       .getFacet(MavenCoreFacet.class).executeMaven(compileArgs);
    }
 
    @Override
    public boolean install()
    {
       return true;
    }
 
    @Override
    public boolean isInstalled()
    {
       return true;
    }
 
    private static class CommandBuilder
    {
       private final List<Stringcommands = new ArrayList<String>();
 
       public static CommandBuilder getBuilder()
       {
          CommandBuilder builder = new CommandBuilder();
          builder.commands.add("exec:java");
          builder.commands.add("-Dexec.classpathScope=test");
 
          return builder;
       }
 
       public CommandBuilder mainClass(final String mainClass)
       {
          .add("-Dexec.mainClass=" + mainClass);
 
          return this;
       }
 
       public CommandBuilder withArguments(final String[] arguments)
       {
          if (arguments.length > 0)
         {
            String quotes = OSUtils.isWindows() ? "\\\"" : "\"";
            StringBuilder argBuilder = new StringBuilder("-Dexec.args=").append(quotes);
            boolean first = true;
            for (String argument : arguments)
            {
               if (!first)
               {
                  argBuilder.append(" ");
               }
               argBuilder.append(argument);
               first = false;
            }
            argBuilder.append(quotes).append(" ");
            .add(argBuilder.toString());
         }
         return this;
      }
      public void setVerbose()
      {
         .add("-X");
      }
      public String[] build()
      {
         String[] args = new String[.size()];
         return .toArray(args);
      }
   }
New to GrepCode? Check out our FAQ X