* @throws MojoExecutionException
*/
public void execute() throws MojoExecutionException
{
getLog().debug( "Using apt compiler" );
Commandline cmd = new Commandline();
int result = APT_COMPILER_SUCCESS;
StringWriter writer = new StringWriter();
// Use reflection to be able to build on all JDKs:
try
{
// init comand line
setAptCommandlineSwitches( cmd );
setAptSpecifics( cmd );
setStandards( cmd );
setClasspath( cmd );
List sourceFiles = new ArrayList();
if ( !fillSourcelist( sourceFiles ) )
{
if ( getLog().isDebugEnabled() )
{
getLog().debug( "there are not stale sources." );
}
return;
}
else
{
if ( fork )
{
if ( !tempRoot.exists() )
{
tempRoot.mkdirs();
}
File file = new File( tempRoot , "files" );
if ( !getLog().isDebugEnabled() )
{
file.deleteOnExit();
}
try
{
FileUtils.fileWrite( file.getAbsolutePath(),
StringUtils.join( sourceFiles.iterator(), "\n" ) );
cmd.createArgument().setValue( '@' + file.getPath() );
}
catch ( IOException e )
{
throw new MojoExecutionException( "Unable to write temporary file for command execution", e );
}
}
else
{
Iterator sourceIt = sourceFiles.iterator();
while ( sourceIt.hasNext() )
{
cmdAdd( cmd, (String) sourceIt.next() );
}
}
}
if ( fork )
{
if ( getLog().isDebugEnabled() )
{
getLog().debug( "Working dir: " + workingDir.getAbsolutePath() );
}
cmd.setWorkingDirectory( workingDir.getAbsolutePath() );
cmd.setExecutable( getAptPath() );
if ( getLog().isDebugEnabled() )
{
getLog().debug( "Invoking apt with cmd " + Commandline.toString( cmd.getShellCommandline() ) );
}
CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
try
{
int exitCode = CommandLineUtils.executeCommandLine( cmd, new DefaultConsumer(), err );
getLog().error( err.getOutput() );
if ( exitCode != 0 )
{
throw new MojoExecutionException( "Exit code: " + exitCode + " - " + err.getOutput() );
}
}
catch ( CommandLineException e )
{
throw new MojoExecutionException( "Unable to execute apt command", e );
}
}
else
{
// we need to have tools.jar in lasspath
// due to bug in Apt compiler, system classpath must be modified but in future:
// TODO try separate ClassLoader (see Plexus compiler api)
if ( !isClasspathModified )
{
URL toolsJar = new File( System.getProperty( "java.home" ),
"../lib/tools.jar" ).toURL();
Method m = URLClassLoader.class.getDeclaredMethod( "addURL",
new Class[] { URL.class } );
m.setAccessible( true );
m.invoke( this.getClass().getClassLoader()
.getSystemClassLoader(), new Object[] { toolsJar } );
isClasspathModified = true;
}
Class c = this.getClass().forName( APT_ENTRY_POINT ); // getAptCompilerClass();
Object compiler = c.newInstance();
if ( getLog().isDebugEnabled() )
{
getLog().debug( "Invoking apt with cmd " + cmd.toString() );
}
try
{
Method compile = c.getMethod( APT_METHOD_NAME, new Class[] {
PrintWriter.class, ( new String[] {} ).getClass() } );
result = ( ( Integer ) //
compile.invoke( compiler, new Object[] { new PrintWriter( writer ),
cmd.getArguments() } ) ).intValue();
}
catch ( NoSuchMethodException e )
{
// ignore
Method compile = c.getMethod( APT_METHOD_NAME_OLD, new Class[] {
( new String[] {} ).getClass(), PrintWriter.class } );
result = ( ( Integer ) //
compile.invoke( compiler, new Object[] {
cmd.getArguments(), new PrintWriter( writer ) } ) ).intValue();
}
}
}