Skip to content

Commit d3fee7f

Browse files
author
carlos
committed
[PLX-314] When forking javac, receive "The input line is too long" error from Windows
1 parent 71b0594 commit d3fee7f

File tree

2 files changed

+93
-11
lines changed

2 files changed

+93
-11
lines changed

plexus-compilers/plexus-compiler-javac/src/main/java/org/codehaus/plexus/compiler/javac/JavacCompiler.java

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@
4949
import org.codehaus.plexus.util.StringUtils;
5050
import org.codehaus.plexus.util.cli.CommandLineException;
5151
import org.codehaus.plexus.util.cli.CommandLineUtils;
52+
import org.codehaus.plexus.util.cli.Commandline;
5253

5354
import java.io.BufferedReader;
5455
import java.io.File;
5556
import java.io.IOException;
5657
import java.io.PrintWriter;
58+
import java.io.FileWriter;
5759
import java.io.StringReader;
5860
import java.io.StringWriter;
5961
import java.lang.reflect.InvocationTargetException;
@@ -312,7 +314,15 @@ List compileOutOfProcess( File workingDirectory, String executable, String[] arg
312314

313315
cli.setExecutable( executable );
314316

315-
cli.addArguments( args );
317+
try
318+
{
319+
File argumentsFile = createFileWithArguments( args );
320+
cli.addArguments( new String[] { "@" + argumentsFile.getCanonicalPath().replace( File.separatorChar, '/' ) } );
321+
}
322+
catch ( IOException e )
323+
{
324+
throw new CompilerException( "Error creating file with javac arguments", e );
325+
}
316326

317327
CommandLineUtils.StringStreamConsumer out = new CommandLineUtils.StringStreamConsumer();
318328

@@ -589,4 +599,44 @@ public static CompilerError parseModernError( String error )
589599
return new CompilerError( "could not parse error message: " + error, isError );
590600
}
591601
}
602+
603+
/**
604+
* put args into a temp file to be referenced using the @ option in javac command line
605+
* @param args
606+
* @return the temporary file wth the arguments
607+
* @throws IOException
608+
*/
609+
private File createFileWithArguments( String[] args )
610+
throws IOException
611+
{
612+
PrintWriter writer = null;
613+
try
614+
{
615+
File tempFile = File.createTempFile( JavacCompiler.class.getName(), "arguments" );
616+
tempFile.deleteOnExit();
617+
618+
writer = new PrintWriter( new FileWriter( tempFile ) );
619+
620+
for ( int i = 0; i < args.length; i++ )
621+
{
622+
String argValue = args[i].replace( File.separatorChar, '/' );
623+
624+
writer.write( "\"" + argValue + "\"" );
625+
626+
writer.write( EOL );
627+
}
628+
629+
writer.flush();
630+
631+
return tempFile;
632+
633+
}
634+
finally
635+
{
636+
if ( writer != null )
637+
{
638+
writer.close();
639+
}
640+
}
641+
}
592642
}

plexus-compilers/plexus-compiler-javac/src/test/java/org/codehaus/plexus/compiler/javac/JavacCompilerTest.java

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,21 @@
2424
* SOFTWARE.
2525
*/
2626

27-
import org.codehaus.plexus.compiler.AbstractCompilerTest;
28-
import org.codehaus.plexus.compiler.CompilerConfiguration;
29-
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
30-
import org.apache.maven.artifact.repository.DefaultArtifactRepository;
31-
import org.apache.maven.artifact.repository.ArtifactRepository;
32-
import org.apache.maven.artifact.Artifact;
33-
import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader;
34-
import org.apache.maven.settings.Settings;
35-
import org.apache.maven.model.Repository;
36-
3727
import java.io.File;
3828
import java.io.FileReader;
3929
import java.util.ArrayList;
4030
import java.util.LinkedHashMap;
4131
import java.util.List;
4232

33+
import org.apache.maven.artifact.Artifact;
34+
import org.apache.maven.artifact.repository.ArtifactRepository;
35+
import org.apache.maven.artifact.repository.DefaultArtifactRepository;
36+
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
37+
import org.apache.maven.settings.Settings;
38+
import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader;
39+
import org.codehaus.plexus.compiler.AbstractCompilerTest;
40+
import org.codehaus.plexus.compiler.CompilerConfiguration;
41+
4342
/**
4443
* @author <a href="mailto:jason@plexus.org">Jason van Zyl</a>
4544
* @version $Id$
@@ -163,6 +162,39 @@ public void testBuildCompilerArgsUnspecifiedVersion()
163162
internalTest( compilerConfiguration, expectedArguments );
164163
}
165164

165+
public void testCommandLineTooLongWhenForking()
166+
throws Exception
167+
{
168+
List expectedArguments = new ArrayList();
169+
170+
CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
171+
172+
populateArguments( compilerConfiguration, expectedArguments, false, false );
173+
174+
compilerConfiguration.setFork( true );
175+
176+
internalTest( compilerConfiguration, expectedArguments );
177+
178+
JavacCompiler compiler = (JavacCompiler) lookup( org.codehaus.plexus.compiler.Compiler.ROLE, getRoleHint() );
179+
180+
File destDir = new File( "target/test-classes-cmd" );
181+
destDir.mkdirs();
182+
183+
/* fill the cmd line arguments, 300 is enough to make it break */
184+
String[] args = new String[400];
185+
args[0] = "-d";
186+
args[1] = destDir.getAbsolutePath();
187+
for ( int i = 2; i < args.length; i++ )
188+
{
189+
args[i] = "org/codehaus/foo/Person.java";
190+
}
191+
192+
List messages = compiler.compileOutOfProcess( new File( getBasedir() + "/src/test-input/src/main" ), "javac",
193+
args );
194+
195+
assertEquals( "There were errors launching the external compiler: " + messages, 0, messages.size() );
196+
}
197+
166198
private void populateArguments( CompilerConfiguration compilerConfiguration, List expectedArguments,
167199
boolean suppressSourceVersion, boolean suppressEncoding )
168200
{

0 commit comments

Comments
 (0)