[java] pom에 정의 된 Maven 속성에 액세스

일반 Maven 프로젝트 및 Maven 플러그인 프로젝트에서 pom에 정의 된 Maven 속성에 어떻게 액세스합니까?



답변

properties-maven-plugin 을 사용하여 properties컴파일 타임에 특정 pom 을 파일에 쓴 다음 런타임에 해당 파일을 읽습니다.

당신에 pom.xml 파일 :

<properties>
     <name>${project.name}</name>
     <version>${project.version}</version>
     <foo>bar</foo>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>properties-maven-plugin</artifactId>
            <version>1.0.0</version>
            <executions>
                <execution>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>write-project-properties</goal>
                    </goals>
                    <configuration>
                        <outputFile>${project.build.outputDirectory}/my.properties</outputFile>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

그리고 .java에서 :

java.io.InputStream is = this.getClass().getResourceAsStream("my.properties");
java.util.Properties p = new Properties();
p.load(is);
String name = p.getProperty("name");
String version = p.getProperty("version");
String foo = p.getProperty("foo");


답변

Maven 및 Java 에서 시스템 변수 설정 다음 호출

System.getProperty("Key");


답변

이는 속성에 대한 maven-resource-plugin필터링이 활성화 된 상태 에서 표준 Java 속성을 사용하여 수행 할 수 있습니다 .

자세한 내용은 http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html을 참조하십시오 .

이것은 플러그인 프로젝트와 마찬가지로 표준 maven 프로젝트에서 작동합니다.


답변

Maven에는 이미 원하는 작업을 수행 할 수있는 솔루션이 있습니다.

POM.xml에서 MavenProject를 가져옵니다-pom 파서?

btw : Google 검색에서 첫 번째 히트;)

Model model = null;
FileReader reader = null;
MavenXpp3Reader mavenreader = new MavenXpp3Reader();

try {
     reader = new FileReader(pomfile); // <-- pomfile is your pom.xml
     model = mavenreader.read(reader);
     model.setPomFile(pomfile);
}catch(Exception ex){
     // do something better here
     ex.printStackTrace()
}

MavenProject project = new MavenProject(model);
project.getProperties() // <-- thats what you need


답변

JDOM (http://www.jdom.org/)을 사용하여 pom 파일을 구문 분석 할 수 있습니다.


답변