使用classpath加载属性文件:resources / jdbc.properties

时间:2015-12-24 18:23:46

标签: java maven url classpath

如果我从命令行执行以下命令 -

,如何使用核心java加载属性文件
css_content = get_inline_content_and_remove_tags(webpage_content, 'style')

js_content = get_inline_content_and_remove_tags(webpage_content, 'script')

webpage_content = # ...clean cruft...

def copy_paste_to_hidden_element(content=None, html_id=None):
    pyperclip.copy(content)
    activate_hidden_element(html_id=html_id, driver=driver)
    call_sp('xdotool key from+ctrl+v')
    time.sleep(1)

copy_paste_to_hidden_element(content=webpage_content, html_id="panel_html")
copy_paste_to_hidden_element(content=js_content, html_id="panel_js")
copy_paste_to_hidden_element(content=css_content, html_id="panel_css")

和在类文件中我使用

mvn clean package exec:java -Dexec.mainClass="com.crossover.trial.properties.Main" -Dexec.args="classpath:resources/jdbc.properties"

它给出了空指针。

建议

2 个答案:

答案 0 :(得分:0)

exec:java" -Dexec.args = param1 param2 param3"

所以在你的情况下exec:java" -Dexec.args = classpath:resources / jdbc.properties"

(查看""位置),您将在args [0]获得classpath:resources / jdbc.properties。

答案 1 :(得分:0)

您不需要在命令行上提供任何位置。或者使用" classpath:"启动资源名称。正如您在回答我之前的评论时所述,使用src / main / resources下的属性文件,maven-jar-plugin会将它放在类路径的jar中。

此代码:

package com.crossover.trial.properties;
import java.net.URL;
import java.util.Properties;
public class Main {
    public static void main(String[] args) {
        Properties prop1 = new Properties();
        URL url1 = ClassLoader.getSystemResource("jdbc.properties");
        try{
            prop1.load(url1.openStream());
        }catch(Exception e)
        {
            e.printStackTrace();
        }
        System.out.println("------- By Using URL class -------");
        System.out.println(prop1);
    }
}

会好的。

似乎您还没有在清单中指定主类。要简化命令行,请使用maven-jar-plugin添加它。

你也可以使用maven-antrun-plugin来启动它(因为我在Eclipse下工作,我避免使用exec-maven-plugin,但除此之外它只是一个品味问题):

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>dummy</groupId>
    <artifactId>dummy</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <main.class>com.crossover.trial.properties.Main</main.class>

        <jdk.version>1.6</jdk.version>
        <project.encoding>UTF-8</project.encoding>

        <project.build.sourceEncoding>${project.encoding}</project.build.sourceEncoding>
        <project.reporting.outputEncoding>${project.encoding}</project.reporting.outputEncoding>

        <maven.compiler.source>${jdk.version}</maven.compiler.source>
        <maven.compiler.target>${jdk.version}</maven.compiler.target>
        <maven.compiler.compilerVersion>${jdk.version}</maven.compiler.compilerVersion>
        <maven.compiler.fork>true</maven.compiler.fork>
        <maven.compiler.verbose>true</maven.compiler.verbose>
        <maven.compiler.optimize>true</maven.compiler.optimize>
        <maven.compiler.debug>true</maven.compiler.debug>

        <maven.jar.plugin.version>2.6</maven.jar.plugin.version>
        <maven.antrun.plugin.version>1.8</maven.antrun.plugin.version>
    </properties>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>${maven.jar.plugin.version}</version>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>${main.class}</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <profiles>
        <profile>
            <id>jar-launcher</id>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <version>${maven.antrun.plugin.version}</version>
                        <configuration>
                            <target>
                                <java fork="true" jar="${project.build.directory}/${project.build.finalName}.jar" />
                            </target>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>class-launcher</id>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <version>${maven.antrun.plugin.version}</version>
                        <configuration>
                            <target>
                                <java fork="true" classname="${main.class}">
                                    <classpath refid="maven.compile.classpath" />
                                </java>
                            </target>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

</project>

现在您可以使用以下命令启动jar:

mvn clean package antrun:run -Pjar-launcher

或直接主类,不包装:

mvn clean compile antrun:run -Pclass-launcher

那就是它。

更新

要使用exec-maven-plugin,请添加到pom:

<profile>
    <id>jar-exec</id>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>${maven.exec.plugin.version}</version>
                <configuration>
                    <executable>java</executable>
                    <arguments>
                        <argument>-jar</argument>
                        <argument>${project.build.directory}/${project.build.finalName}.jar</argument>
                    </arguments>
                </configuration>
            </plugin>
        </plugins>
    </build>
</profile>
<profile>
    <id>class-exec</id>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>${maven.exec.plugin.version}</version>
                <configuration>
                    <executable>java</executable>
                    <arguments>
                        <argument>-classpath</argument>
                        <classpath />
                        <argument>${main.class}</argument>
                    </arguments>
                </configuration>
            </plugin>
        </plugins>
    </build>
</profile>

现在您可以使用以下命令启动jar:

mvn clean package exec:exec -Pjar-exec

或直接主类,不包装:

mvn clean compile exec:exec -Pclass-exec