Debian Codegraph @

animal-sniffer-maven-plugin/src/main/java/org/codehaus/mojo/animal_sniffer/maven/BuildSignaturesMojo.java

raw
1 package org.codehaus.mojo.animal_sniffer.maven;
2
3 /*
4 * The MIT License
5 *
6 * Copyright (c) 2009, codehaus.org
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 *
26 */
27
28 import org.apache.maven.artifact.Artifact;
29 import org.apache.maven.artifact.ArtifactUtils;
30 import org.apache.maven.execution.MavenSession;
31 import org.apache.maven.plugin.AbstractMojo;
32 import org.apache.maven.plugin.MojoExecutionException;
33 import org.apache.maven.plugin.MojoFailureException;
34 import org.apache.maven.plugins.annotations.Component;
35 import org.apache.maven.plugins.annotations.Mojo;
36 import org.apache.maven.plugins.annotations.Parameter;
37 import org.apache.maven.plugins.annotations.ResolutionScope;
38 import org.apache.maven.project.MavenProject;
39 import org.apache.maven.project.MavenProjectHelper;
40 import org.apache.maven.shared.artifact.filter.PatternExcludesArtifactFilter;
41 import org.apache.maven.shared.artifact.filter.PatternIncludesArtifactFilter;
42 import org.apache.maven.toolchain.MisconfiguredToolchainException;
43 import org.apache.maven.toolchain.Toolchain;
44 import org.apache.maven.toolchain.ToolchainManager;
45 import org.apache.maven.toolchain.ToolchainManagerPrivate;
46 import org.apache.maven.toolchain.ToolchainPrivate;
47 import org.codehaus.mojo.animal_sniffer.SignatureBuilder;
48 import org.codehaus.plexus.util.StringUtils;
49 import org.codehaus.plexus.util.cli.CommandLineException;
50 import org.codehaus.plexus.util.cli.CommandLineUtils;
51 import org.codehaus.plexus.util.cli.Commandline;
52
53 import java.io.File;
54 import java.io.FileInputStream;
55 import java.io.FileNotFoundException;
56 import java.io.FileOutputStream;
57 import java.io.IOException;
58 import java.io.InputStream;
59 import java.lang.reflect.InvocationTargetException;
60 import java.lang.reflect.Method;
61 import java.util.ArrayList;
62 import java.util.Arrays;
63 import java.util.Iterator;
64 import java.util.List;
65
66 /**
67 * Generates an API Signature from at least one of: the java runtime, the
68 * module dependencies and the module classes.
69 *
70 * @author Stephen Connolly
71 */
72 @Mojo( name = "build", configurator = "override", requiresDependencyResolution = ResolutionScope.COMPILE, threadSafe = true )
73 public class BuildSignaturesMojo
74 extends AbstractMojo
75 {
76 /**
77 * Should the signatures from java home be included.
78 *
79 * @since 1.3
80 */
81 @Parameter( property = "includeJavaHome", defaultValue = "true" )
82 private boolean includeJavaHome;
83
84 /**
85 * Should no signatures be generated if no java home is available.
86 *
87 * @since 1.3
88 */
89 @Parameter( property = "skipIfNoJavaHome", defaultValue = "false" )
90 private boolean skipIfNoJavaHome;
91
92 /**
93 * Should the signatures from this module's classes be included..
94 *
95 * @since 1.3
96 */
97 @Parameter( property = "includeJavaHome", defaultValue = "true" )
98 private boolean includeModuleClasses;
99
100 /**
101 * Classes to generate signatures of.
102 *
103 * @since 1.3
104 */
105 @Parameter
106 private String[] includeClasses = null;
107
108 /**
109 * Classes to exclude from generating signatures of.
110 *
111 * @since 1.3
112 */
113 @Parameter
114 private String[] excludeClasses = null;
115
116 /**
117 * A list of artifact patterns to include. Patterns can include <code>*</code> as a wildcard match for any
118 * <b>whole</b> segment, valid patterns are:
119 * <ul>
120 * <li><code>groupId:artifactId</code></li>
121 * <li><code>groupId:artifactId:type</code></li>
122 * <li><code>groupId:artifactId:type:version</code></li>
123 * <li><code>groupId:artifactId:type:classifier</code></li>
124 * <li><code>groupId:artifactId:type:classifier:version</code></li>
125 * </ul>
126 *
127 * @since 1.3
128 */
129 @Parameter
130 private String[] includeDependencies = null;
131
132 /**
133 * A list of artifact patterns to exclude. Patterns can include <code>*</code> as a wildcard match for any
134 * <b>whole</b> segment, valid patterns are:
135 * <ul>
136 * <li><code>groupId:artifactId</code></li>
137 * <li><code>groupId:artifactId:type</code></li>
138 * <li><code>groupId:artifactId:type:version</code></li>
139 * <li><code>groupId:artifactId:type:classifier</code></li>
140 * <li><code>groupId:artifactId:type:classifier:version</code></li>
141 * </ul>
142 *
143 * @since 1.3
144 */
145 @Parameter
146 private String[] excludeDependencies = null;
147
148 /**
149 * The java home to generate the signatures of, if not specified only the signatures of dependencies will be
150 * included. This parameter is overridden by {@link #javaHomeClassPath}. This parameter overrides {@link #jdk}
151 * and any java home specified by maven-toolchains-plugin.
152 *
153 * @since 1.3
154 */
155 @Parameter( property = "javaHome" )
156 private String javaHome;
157
158 /**
159 * Use this configuration option only if the automatic boot classpath detection does not work for the specific
160 * {@link #javaHome} or {@link #jdk}. For example, the automatic boot classpath detection does not work with
161 * Sun Java 1.1. This parameter overrides {@link #javaHome}, {@link #jdk} and the maven-toolchains-plugin.
162 *
163 * @since 1.3
164 */
165 @Parameter
166 private File[] javaHomeClassPath;
167
168 /**
169 * Where to put the generated signatures.
170 *
171 * @since 1.3
172 */
173 @Parameter( defaultValue = "${project.build.directory}", required = true )
174 private File outputDirectory;
175
176 /**
177 * Where to find this modules classes.
178 *
179 * @since 1.3
180 */
181 @Parameter( defaultValue = "${project.build.outputDirectory}", required = true )
182 private File classesDirectory;
183
184 /**
185 * The name of the generated signatures.
186 *
187 * @since 1.3
188 */
189 @Parameter( defaultValue = "${project.build.finalName}", required = true )
190 private String signaturesName;
191
192 /**
193 * The classifier to add to the generated signatures.
194 *
195 * @since 1.3
196 */
197 @Parameter
198 private String classifier;
199
200 /**
201 */
202 @Component
203 private MavenProjectHelper projectHelper;
204
205 /**
206 * The maven project.
207 */
208 @Parameter( defaultValue = "${project}", required = true, readonly = true )
209 private MavenProject project;
210
211 /**
212 */
213 @Component
214 private ToolchainManager toolchainManager;
215
216 /**
217 */
218 @Component
219 private ToolchainManagerPrivate toolchainManagerPrivate;
220
221 /**
222 * The current build session instance. This is used for toolchain manager API calls.
223 */
224 @Parameter( defaultValue = "${session}", required = true, readonly = true )
225 private MavenSession session;
226
227 /**
228 * The JDK Toolchain to use. This parameter can be overridden by {@link #javaHome} or {@link #javaHomeClassPath}.
229 * This parameter overrides any toolchain specified with maven-toolchains-plugin.
230 *
231 * @since 1.3
232 */
233 @Parameter
234 private JdkToolchain jdk;
235
236 /**
237 */
238 @Parameter( defaultValue = "${plugin.artifacts}", required = true, readonly = true )
239 private List<Artifact> pluginArtifacts;
240
241 /**
242 * The groupId of the Java Boot Classpath Detector to use. The plugin's dependencies will be searched for a
243 * dependency of type <code>jar</code> with this groupId and the artifactId specified in {@link #jbcpdArtifactId}.
244 * The dependency should be a standalone executable jar file which outputs the java boot classpath as a single
245 * line separated using {@link File#pathSeparatorChar} or else exits with a non-zero return code if it cannot determine
246 * the java boot classpath.
247 *
248 * @since 1.3
249 */
250 @Parameter( defaultValue = "${plugin.groupId}" )
251 private String jbcpdGroupId;
252
253 /**
254 * The artifactId of the Java Boot Classpath Detector to use. The plugin's dependencies will be searched for a
255 * dependency of type <code>jar</code> with this artifactId and the groupId specified in {@link #jbcpdGroupId}.
256 * The dependency should be a standalone executable jar file which outputs the java boot classpath as a single
257 * line separated using {@link File#pathSeparatorChar} or else exits with a non-zero return code if it cannot determine
258 * the java boot classpath.
259 *
260 * @since 1.3
261 */
262 @Parameter( defaultValue = "java-boot-classpath-detector" )
263 private String jbcpdArtifactId;
264
265 public void execute()
266 throws MojoExecutionException, MojoFailureException
267 {
268 if ( includeJavaHome && ( javaHomeClassPath == null || javaHomeClassPath.length == 0 ) )
269 {
270 if ( javaHome != null )
271 {
272 getLog().warn( "Toolchains are ignored, 'javaHome' parameter is set to " + javaHome );
273
274 if ( !new File( javaHome ).isDirectory() )
275 {
276 if ( skipIfNoJavaHome )
277 {
278 getLog().warn( "Skipping signature generation as java home (" + javaHome + ") does not exist" );
279 return;
280 }
281 throw new MojoFailureException( "Cannot include java home if specified java home does not exist" );
282 }
283
284 if ( !detectJavaBootClasspath( new File( new File( javaHome, "bin" ), "java" ).getAbsolutePath() ) )
285 {
286 return;
287 }
288 }
289 else
290 {
291 Toolchain tc = getJdkToolchain();
292
293 if ( tc == null && jdk == null )
294 {
295 String jvm = null;
296 // TODO: how could this give non null result here if tc was null when doing same???
297 tc = toolchainManager.getToolchainFromBuildContext( "jdk", //NOI18N
298 session );
299 getLog().info( "Toolchain from session: " + tc );
300 //assign the path to executable from toolchains
301 if ( tc != null )
302 {
303 jvm = tc.findTool( "java" ); //NOI18N
304 }
305
306 if ( jvm == null )
307 {
308 if ( skipIfNoJavaHome )
309 {
310 getLog().warn( "Skipping signature generation as could not find java home" );
311 return;
312 }
313 throw new MojoFailureException(
314 "Cannot include java home if java home is not specified (either via javaClassPath, javaHome or jdk)" );
315 }
316 if ( !detectJavaBootClasspath( jvm ) )
317 {
318 return;
319 }
320 }
321 else if ( tc != null && StringUtils.equalsIgnoreCase( "jdk",tc.getType() ) ) //tc instanceof JavaToolChain )
322 {
323 getLog().info( "Toolchain in animal-sniffer-maven-plugin: " + tc );
324
325 //when the executable to use is explicitly set by user in mojo's parameter, ignore toolchains.
326
327 //assign the path to executable from toolchains
328 String jvm = tc.findTool( "java" ); //NOI18N
329
330 if ( jvm == null )
331 {
332 if ( skipIfNoJavaHome )
333 {
334 getLog().warn( "Skipping signature generation as could not find java home" );
335 return;
336 }
337 throw new MojoFailureException(
338 "Cannot include java home if java home is not specified (either via javaClassPath, javaHome or jdk)" );
339 }
340 if ( !detectJavaBootClasspath( jvm ) )
341 {
342 return;
343 }
344 }
345 else if ( tc == null && jdk != null && jdk.getParameters() != null )
346 {
347 if ( skipIfNoJavaHome )
348 {
349 getLog().warn( "Skipping signature generation as could not find jdk toolchain to match "
350 + jdk.getParameters() );
351 return;
352 }
353 throw new MojoFailureException( "Could not find jdk toolchain to match " + jdk.getParameters() );
354 }
355 else
356 {
357 if ( skipIfNoJavaHome )
358 {
359 getLog().warn( "Skipping signature generation as could not find java home" );
360 return;
361 }
362 throw new MojoFailureException(
363 "Cannot include java home if java home is not specified (either via javaClassPath, javaHome, "
364 + "jdk or maven-toolchains-plugin)" );
365 }
366 }
367 }
368
369 displayJavaBootClasspath();
370
371 File sigFile = getTargetFile( outputDirectory, signaturesName, classifier, "signature" );
372 try
373 {
374 outputDirectory.mkdirs();
375 SignatureBuilder builder = new SignatureBuilder( getBaseSignatures(), new FileOutputStream( sigFile ),
376 new MavenLogger( getLog() ) );
377
378 if ( includeClasses != null )
379 {
380 getLog().info( "Restricting signatures to include only the following classes:" );
381 for ( int i = 0; i < includeClasses.length; i++ )
382 {
383 getLog().info( " " + includeClasses[i] );
384 builder.addInclude( includeClasses[i] );
385 }
386 }
387
388 if ( excludeClasses != null )
389 {
390 getLog().info( "Restricting signatures to exclude the following classes:" );
391 for ( int i = 0; i < excludeClasses.length; i++ )
392 {
393 getLog().info( " " + excludeClasses[i] );
394 builder.addExclude( excludeClasses[i] );
395 }
396 }
397
398 processJavaBootClasspath( builder );
399
400 processModuleDependencies( builder );
401
402 processModuleClasses( builder );
403
404 builder.close();
405
406 projectHelper.attachArtifact( project, "signature", classifier, sigFile );
407
408 }
409 catch ( IOException e )
410 {
411 throw new MojoExecutionException( e.getMessage(), e );
412 }
413 }
414
415 private boolean detectJavaBootClasspath( String javaExecutable )
416 throws MojoFailureException, MojoExecutionException
417 {
418 getLog().info( "Attempting to auto-detect the boot classpath for " + javaExecutable );
419 Iterator<Artifact> i = pluginArtifacts.iterator();
420 Artifact javaBootClasspathDetector = null;
421 while ( i.hasNext() && javaBootClasspathDetector == null )
422 {
423 Artifact candidate = (Artifact) i.next();
424
425 if ( StringUtils.equals( jbcpdGroupId, candidate.getGroupId() )
426 && StringUtils.equals( jbcpdArtifactId, candidate.getArtifactId() ) && candidate.getFile() != null
427 && candidate.getFile().isFile() )
428 {
429 javaBootClasspathDetector = candidate;
430 }
431 }
432 if ( javaBootClasspathDetector == null )
433 {
434 if ( skipIfNoJavaHome )
435 {
436 getLog().warn( "Skipping signature generation as could not find boot classpath detector ("
437 + ArtifactUtils.versionlessKey( jbcpdGroupId, jbcpdArtifactId ) + ")." );
438 return false;
439 }
440 throw new MojoFailureException( "Could not find boot classpath detector ("
441 + ArtifactUtils.versionlessKey( jbcpdGroupId, jbcpdArtifactId )
442 + ")." );
443 }
444
445 try
446 {
447 if ( !detectJavaClasspath( javaBootClasspathDetector, javaExecutable ) )
448 {
449 return false;
450 }
451 }
452 catch ( CommandLineException e )
453 {
454 throw new MojoExecutionException( e.getLocalizedMessage(), e );
455 }
456 return true;
457 }
458
459 private boolean detectJavaClasspath( Artifact javaBootClasspathDetector, String javaExecutable )
460 throws CommandLineException, MojoFailureException
461 {
462 final Commandline cli = new Commandline();
463 cli.setWorkingDirectory( project.getBasedir().getAbsolutePath() );
464 cli.setExecutable( javaExecutable );
465 cli.addEnvironment( "CLASSPATH", "" );
466 cli.addEnvironment( "JAVA_HOME", "" );
467 cli.addArguments( new String[]{ "-jar", javaBootClasspathDetector.getFile().getAbsolutePath() } );
468
469 final CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
470 final CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
471 int exitCode = CommandLineUtils.executeCommandLine( cli, stdout, stderr );
472 if ( exitCode != 0 )
473 {
474 getLog().debug( "Stdout: " + stdout.getOutput() );
475 getLog().debug( "Stderr: " + stderr.getOutput() );
476 getLog().debug( "Exit code = " + exitCode );
477 if ( skipIfNoJavaHome )
478 {
479 getLog().warn( "Skipping signature generation as could not auto-detect java boot classpath for "
480 + javaExecutable );
481 return false;
482 }
483 throw new MojoFailureException( "Could not auto-detect java boot classpath for " + javaExecutable );
484 }
485 String[] classpath = StringUtils.split( stdout.getOutput(), File.pathSeparator );
486 javaHomeClassPath = new File[classpath.length];
487 for ( int j = 0; j < classpath.length; j++ )
488 {
489 javaHomeClassPath[j] = new File( classpath[j] );
490 }
491 return true;
492 }
493
494 private void displayJavaBootClasspath()
495 {
496 if ( includeJavaHome )
497 {
498 getLog().info( "Java Classpath:" );
499 if ( javaHomeClassPath == null )
500 {
501 getLog().info( " Empty" );
502 }
503 else
504 {
505 for ( int j = 0; j < javaHomeClassPath.length; j++ )
506 {
507 getLog().info( " [" + j + "] = " + javaHomeClassPath[j] );
508 }
509 }
510 }
511 }
512
513 private void processModuleDependencies( SignatureBuilder builder )
514 throws IOException
515 {
516 PatternIncludesArtifactFilter includesFilter = includeDependencies == null
517 ? null
518 : new PatternIncludesArtifactFilter( Arrays.asList( includeDependencies ) );
519 PatternExcludesArtifactFilter excludesFilter = excludeDependencies == null
520 ? null
521 : new PatternExcludesArtifactFilter( Arrays.asList( excludeDependencies ) );
522
523 for ( Iterator<Artifact> i = project.getArtifacts().iterator(); i.hasNext(); )
524 {
525 Artifact artifact = i.next();
526
527 if ( includesFilter != null && !includesFilter.include( artifact ) )
528 {
529 getLog().debug( "Artifact " + artifactId( artifact ) + " ignored as it does not match include rules." );
530 continue;
531 }
532
533 if ( excludesFilter != null && !excludesFilter.include( artifact ) )
534 {
535 getLog().debug( "Artifact " + artifactId( artifact ) + " ignored as it does match exclude rules." );
536 continue;
537 }
538
539 if ( artifact.getArtifactHandler().isAddedToClasspath() )
540 {
541 getLog().info( "Parsing signatures from " + artifactId( artifact ) );
542 builder.process( artifact.getFile() );
543 }
544
545 }
546 }
547
548 private void processModuleClasses( SignatureBuilder builder )
549 throws IOException
550 {
551 if ( includeModuleClasses && classesDirectory.isDirectory() )
552 {
553 getLog().info( "Parsing signatures from " + classesDirectory );
554 builder.process( classesDirectory );
555 }
556 }
557
558 private void processJavaBootClasspath( SignatureBuilder builder )
559 throws IOException
560 {
561 if ( includeJavaHome && javaHomeClassPath != null && javaHomeClassPath.length > 0 )
562 {
563 getLog().debug( "Parsing signatures java classpath:" );
564 for ( int i = 0; i < javaHomeClassPath.length; i++ )
565 {
566 if ( javaHomeClassPath[i].isFile() || javaHomeClassPath[i].isDirectory() )
567 {
568 getLog().debug( "Processing " + javaHomeClassPath[i] );
569 builder.process( javaHomeClassPath[i] );
570 }
571 else
572 {
573 getLog().warn( "Could not add signatures from boot classpath element: " + javaHomeClassPath[i]
574 + " as it does not exist." );
575 }
576 }
577 }
578 }
579
580 private InputStream[] getBaseSignatures()
581 throws FileNotFoundException
582 {
583 List<InputStream> baseSignatures = new ArrayList<InputStream>();
584 for ( Iterator<Artifact> i = project.getArtifacts().iterator(); i.hasNext(); )
585 {
586 Artifact artifact = i.next();
587 if ( StringUtils.equals( "signature", artifact.getType() ) )
588 {
589 getLog().info( "Importing sigantures from " + artifact.getFile() );
590 baseSignatures.add( new FileInputStream( artifact.getFile() ) );
591 }
592 }
593 final InputStream[] baseSignatureInputStreams =
594 (InputStream[]) baseSignatures.toArray( new InputStream[baseSignatures.size()] );
595 return baseSignatureInputStreams;
596 }
597
598 /**
599 * Gets the <code>jdk</code> toolchain to use.
600 *
601 * @return the <code>jdk</code> toolchain to use or <code>null</code> if no toolchain is configured or if no toolchain can be found.
602 * @throws MojoExecutionException if toolchains are misconfigured.
603 */
604 private Toolchain getJdkToolchain()
605 throws MojoExecutionException
606 {
607 Toolchain tc = getJdkToolchainFromConfiguration();
608 if ( tc == null )
609 {
610 tc = getJdkToolchainFromContext();
611 }
612 return tc;
613 }
614
615 /**
616 * Gets the toolchain specified for the current context, e.g. specified via the maven-toolchain-plugin
617 *
618 * @return the toolchain from the context or <code>null</code> if there is no such toolchain.
619 */
620 private Toolchain getJdkToolchainFromContext()
621 {
622 if ( toolchainManager != null )
623 {
624 return toolchainManager.getToolchainFromBuildContext( "jdk", //NOI18N
625 session );
626 }
627 return null;
628 }
629
630 /**
631 * Gets the <code>jdk</code> toolchain from this plugin's configuration.
632 *
633 * @return the toolchain from this plugin's configuration, or <code>null</code> if no matching toolchain can be
634 * found.
635 * @throws MojoExecutionException if the toolchains are configured incorrectly.
636 */
637 private Toolchain getJdkToolchainFromConfiguration()
638 throws MojoExecutionException
639 {
640 if ( toolchainManager != null && jdk != null && jdk.getParameters() != null )
641 {
642 try
643 {
644 final ToolchainPrivate[] tcp = getToolchains( "jdk" );
645 for ( int i = 0; i < tcp.length; i++ )
646 {
647 if ( tcp[i].getType().equals( "jdk" ) /* MNG-5716 */
648 && tcp[i].matchesRequirements( jdk.getParameters() ) )
649 {
650 return tcp[i];
651 }
652 }
653 }
654 catch ( MisconfiguredToolchainException e )
655 {
656 throw new MojoExecutionException( e.getLocalizedMessage(), e );
657 }
658 }
659 return null;
660 }
661
662 /*package*/ static String artifactId( Artifact artifact )
663 {
664 return artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getType()
665 + ( artifact.getClassifier() != null ? ":" + artifact.getClassifier() : "" ) + ":"
666 + artifact.getBaseVersion();
667
668 }
669
670 private ToolchainPrivate[] getToolchains( String type )
671 throws MojoExecutionException, MisconfiguredToolchainException
672 {
673 // The toolchain API has moved about quite a bit... this method tries to navigate through to a
674 // successful enumeration of all the toolchains of the required type.
675 // This method is only ever called in versions of Maven that have toolchain support.
676
677 Class<?> managerClass = toolchainManagerPrivate.getClass();
678 try
679 {
680 try
681 {
682 // try 3.x style API
683 Method newMethod =
684 managerClass.getMethod( "getToolchainsForType", new Class[]{ String.class, MavenSession.class } );
685
686 return (ToolchainPrivate[]) newMethod.invoke( toolchainManagerPrivate, new Object[]{ type, session } );
687 }
688 catch ( NoSuchMethodException e1 )
689 {
690 try
691 {
692 // try 2.2.1 style API
693 Method oldMethod = managerClass.getMethod( "getToolchainsForType", new Class[]{ String.class } );
694
695 return (ToolchainPrivate[]) oldMethod.invoke( toolchainManagerPrivate, new Object[]{ type } );
696 }
697 catch ( NoSuchMethodException e2 )
698 {
699 e2.initCause( e1 );
700 throw e2;
701 }
702 }
703 }
704 catch ( NoSuchMethodException e )
705 {
706 StringBuilder buf = new StringBuilder( "Incompatible toolchain API." );
707 buf.append( "\n\nCannot find a suitable 'getToolchainsForType' method. Available methods are:\n" );
708
709 Method[] methods = managerClass.getMethods();
710 for ( int i = 0; i < methods.length; i++ )
711 {
712 buf.append( " " ).append( methods[i] ).append( '\n' );
713 }
714 throw new MojoExecutionException( buf.toString(), e );
715 }
716 catch ( IllegalAccessException e )
717 {
718 throw new MojoExecutionException( "Incompatible toolchain API", e );
719 }
720 catch ( InvocationTargetException e )
721 {
722 Throwable cause = e.getCause();
723
724 if ( cause instanceof RuntimeException )
725 {
726 throw (RuntimeException) cause;
727 }
728 if ( cause instanceof MisconfiguredToolchainException )
729 {
730 throw (MisconfiguredToolchainException) cause;
731 }
732
733 throw new MojoExecutionException( "Incompatible toolchain API", e );
734 }
735 }
736
737 private static File getTargetFile( File basedir, String finalName, String classifier, String type )
738 {
739 if ( classifier == null )
740 {
741 classifier = "";
742 }
743 else if ( classifier.trim().length() > 0 && !classifier.startsWith( "-" ) )
744 {
745 classifier = "-" + classifier;
746 }
747
748 return new File( basedir, finalName + classifier + "." + type );
749 }
750
751 }