简体   繁体   English

从POM.xml文件读取始终为空(Maven,Java)

[英]Reading from POM.xml file is always null (Maven, java)

I want read from pom.xml file in my java app. 我想从我的java应用程序中的pom.xml文件读取。

I read a lot of article about this problem, but solution not work. 我阅读了很多有关此问题的文章,但解决方案不起作用。

I make test class for testing: 我制作测试类进行测试:

public class test {
    public static void main(String[] args) {
        System.out.println(System.getProperty("database.username"));
    }
}

pom.xml file: pom.xml文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <!-- I HIDE PERSONAL DATA -->

    <properties>
        <junit.version>3.8.1</junit.version>
        <mysql.connector.version>5.1.34</mysql.connector.version>
        <commons.lang3.version>3.3.2</commons.lang3.version>
        <database.username>nobodyTest</database.username>    
    </properties>

    <dependencies>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.connector.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${commons.lang3.version}</version>
        </dependency>


        <!-- Servlet Stuff -->

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
            <scope>provided</scope>
        </dependency>

        <!-- JPA - Hibernate stuff -->

        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.0.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.3.8.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-c3p0</artifactId>
            <version>3.6.3.Final</version>
        </dependency>

        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.5.0-b01</version>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.2.11.v20150529</version>
                <configuration>
                    <scanIntervalSeconds>1</scanIntervalSeconds>
                    <webApp>
                        <contextPath>/</contextPath>
                    </webApp>
                </configuration>
            </plugin>

        </plugins>
    </build>
</project>

But if i run the code, i get always null . 但是,如果我运行代码,我将始终为null Why? 为什么?

If you use a maven goal or profile to execute your code, I suggest you to use the exec-maven-plugin to run the class you want and you will be able to set any paramaters you need in your main class. 如果您使用Maven目标或配置文件执行代码,建议您使用exec-maven-plugin运行所需的类,并且可以在主类中设置所需的任何参数。 See http://www.mojohaus.org/exec-maven-plugin/ 参见http://www.mojohaus.org/exec-maven-plugin/

Otherwise, you can write a custom launch script (in batch for example) into a maven resources directory (for property filtering) 否则,您可以将自定义启动脚本(例如,分批)编写到Maven资源目录中(用于属性过滤)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM