4

実行時に、アプリからmavenjaxb2プラグインを実行する必要があります。出来ますか?

4

2 に答える 2

4

多分私はあなたを助けることができる何かをしました:

/**
 * @author swoeste
 * 
 */
public class MavenExecutor {

private final File       configuration;
private final ClassWorld classWorld;

/**
 * Constructor for a new maven executor.
 * 
 * @param aConfiguration
 */
public MavenExecutor( final File aConfiguration ) {
    this.configuration = aConfiguration;
    this.classWorld = new ClassWorld( "plexus.core", this.getClass().getClassLoader() ); //$NON-NLS-1$
}

/**
 * This method is used to perform a mvn  command on the given pom. The original
 * command must be given, also the sub folder and the marker folder in the working directory. The working directory
 * and the configuration file will be added before execution.
 * 
 * @param cmd the mvn command to execute
 * @param pom the absolute path to a maven pom file
 * @param output the absolute path to the working directory
 * @param folder the output sub folder of the working directory
 * @param marker the marker sub folder of the working directory
 * @return
 */
public int unpack( final String cmd, final File pom, final File output, final String folder, final String marker ) {
    final List<String> commands = new ArrayList<String>( //
            Arrays.asList( cmd.split( ConfigurationConstants.MAVEN_DELIMITER ) ) );
    commands.add( "-DoutputDirectory=" + output.getAbsolutePath() + folder ); //$NON-NLS-1$
    commands.add( "-DmarkersDirectory=" + output.getAbsolutePath() + marker ); //$NON-NLS-1$
    commands.add( "-gs=\"" + this.configuration.getAbsolutePath() + "\"" ); //$NON-NLS-1$//$NON-NLS-2$
    commands.add( "-f=\"" + pom.getAbsolutePath() + "\"" ); //$NON-NLS-1$ //$NON-NLS-2$
    return MavenCli.doMain( commands.toArray( new String[commands.size()] ), this.classWorld );
}

}

あなたが何をしたいのか正確にはわかりませんが、maven プロジェクトで maven プラグインを実行したい場合は、上記のコードが機能します。私の場合、プロジェクトで mvn dependency:unpack-dependencies コマンドを実行します。

上記を機能させるには、次の依存関係が必要です。

  <dependency>
     <groupId>org.apache.maven</groupId>
     <artifactId>maven-embedder</artifactId>
     <version>3.0.3</version>
  </dependency>

PS: Java から Maven を実行する方法についての良いリソースは、m2eclipse プラグインの実装です ;)

于 2012-04-20T11:15:01.750 に答える
0

私はあなたが本当にそれを必要としています、もちろんあなたはできます。プラグインのソースをダウンロードして、そこで何が起こっているのかを調べてください。適切な Mojo (プラグインのゴールを実装するクラス) をインスタンス化して実行できます。ただし、通常、プラグインの目標は Maven プロジェクトのコンテキストに大きく依存しているため、提供する (または何らかの形でモックすることさえ) 非常に困難な場合があるため、Mojo はエラーなしで実行できました。

あなたの具体的な状況はわかりませんが、プラグインのソースコードで見つけたものに基づいて、達成したいことの独自の実装を作成する方が本当に賢明であると99%確信しています(すべてを書くのではなく自分で)。

于 2012-04-20T11:02:16.987 に答える