2012-04-20 3 views
2

런타임에 내 앱에서 maven jaxb2 플러그인을 실행해야합니다. 가능한가?자바 클래스에서 메이븐 플러그인을 실행할 수 있습니까?

+0

소프트웨어에서) 자바는 m2eclipse에 플러그인의 구현이에서 받는다는 것을 실행하는 방법에 대한 자세한 내용은 좋은 자원 :

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

PS :

는이 종속성을 필요로 작업 위를 얻으려면 모든 것이 가능하다. 약간의 세부 사항을 제공하는 데주의해야합니까? – maksimov

+4

왜 Maven 플러그인을 실행 하시겠습니까? JAXB와 관련된 무언가가 필요하다고 생각하십니까? 그런 다음 JAXB API를 직접 사용하십시오. 나는 그것이 가능하다고 확신하지만, 그것을 작동 시키려면 많은 일이 필요할 것이다. 플러그인은 고립되어 실행될 수 없으며, Maven 환경이 필요하고 가장 중요한 것은 pom.xml이 필요합니다. –

+0

@maksimov 사실 소프트웨어에서 모든 것이 불가능합니다.) – polypiel

답변

2

은 어쩌면 내가 당신에게 도움이 될 수있는 일이 있습니다

/** 
* @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 종속성을 실행합니다 : 프로젝트의 unpack-dependencies 명령.

+0

좋습니다. 솔루션처럼 보입니다. –

0

나는 정말로 그렇게 할 수있다. 플러그인의 소스를 다운로드하고 진행중인 내용을 살펴보십시오. 적절한 Mojo (플러그인의 목표를 구현하는 클래스)를 인스턴스화하고 실행할 수 있습니다. 그러나 일반적으로 플러그인의 목표는 Mojo가 오류없이 실행할 수 있도록 Maven 프로젝트 컨텍스트에 크게 의존합니다 (또는 어떻게 든 모의하기가 어려울 수 있음).

나는 당신의 구체적인 상황을 모른다. 그러나 나는 당신이 달성하기를 원하는, 잠재적으로 당신이 플러그인의 소스 코드에서 발견 한 것들의 자신의 구현을 작성하는 것이 더 현명 할 것이라고 확신한다. (당신 자신의 모든 것을 쓰지 않는 것).

관련 문제