2012-04-20 12 views
2

実行時に、私のアプリからmaven jaxb2プラグインを実行する必要があります。出来ますか?javaクラスからMavenプラグインを実行することはできますか?

+0

すべてが可能です。ケアはいくつかの詳細を提供する? – maksimov

+4

なぜMavenプラグインを実行しますか? JAXBに関連する何かが必要だと思う?次に、JAXB APIを直接使用します。私はそれが可能だと確信していますが、それを働かせるためには多くの仕事が必要です。プラグインは孤立して実行することはできません.Maven環境が必要です。最も重要なのはpom.xmlです。 –

+0

@maksimovソフトウェアではすべてが不可能です;) – polypiel

答えて

2

たぶん私はあなたを助けることができる何かを行っている:私はあなたがしたい正確に何を知っているが、あなたはMavenプロジェクトのMavenプラグインを実行したい場合はいけない

/** 
* @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); 
} 

}

を上記のコードが動作します。私の場合は、mvnの依存関係を実行します:プロジェクトのunpack-dependenciesコマンド。

あなたはこの依存関係を必要とする作業以上を取得するには:

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

PS:Javaからのmavenの事を実行する方法については、優れたリソースはm2eclipseプラグインの実装であり、ソフトウェアで)

+0

ソリューションのように見えます。 –

0

私はあなたが本当に必要とすることができます。プラグインのソースをダウンロードして、そこに何が起こっているのかを見てください。適切なMojo(プラグインのゴールを実装するクラス)をインスタンス化して実行することができます。しかし、通常、プラグインの目標は、Mojoがエラーなく実行できるように、実際には提供するのが難しい(または何とかして模擬する)かもしれないMavenプロジェクトのコンテキストに大きく依存しています。

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

関連する問題