如何使用maven将属性文件打包到jar中

How to pack properties file in jar using maven

我一直在尝试使用 Maven 将属性文件打包到我的 jar 中,但到目前为止没有成功。

项目结构:

src/main/java
    All java source files
src/test/java
    All test java source files
src/main/resource
    All properties files. For example, application.properties
...
pom.xml

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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>Database</groupId>
    <artifactId>Executor</artifactId>
    <version>0.1</version>
    <packaging>jar</packaging>
    <name>DatabaseExecutor</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.framework.version>5.2.0.RELEASE</spring.framework.version>
        <spring.framework.group.id>org.springframework</spring.framework.group.id>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <fully.qualified.main.class>com.applications.App</fully.qualified.main.class>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.14.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.14.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>${spring.framework.group.id}</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.framework.version}</version>
        </dependency>
        <dependency>
            <groupId>${spring.framework.group.id}</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.framework.version}</version>
        </dependency>
        <dependency>
            <groupId>${spring.framework.group.id}</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.framework.version}</version>
        </dependency>
        <dependency>
            <groupId>${spring.framework.group.id}</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.framework.version}</version>
        </dependency>
        <dependency>
            <groupId>${spring.framework.group.id}</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring.framework.version}</version>
        </dependency>
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc14</artifactId>
            <version>12.2.0.1</version>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.1.2</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/libs</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <version>3.2.0</version>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>libs/</classpathPrefix>
                            <mainClass>${fully.qualified.main.class}</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

我创建了一个 build.bat 文件来构建具有以下内容的代码。

@ECHO OFF
call mvn -e pre-clean
call mvn -e clean
call mvn -e compile
call mvn -e package -DskipTests

命令提示符日志:

[INFO] Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< Database:Executor >--------------------------
[INFO] Building DatabaseExecutor 0.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.146 s
[INFO] Finished at: 2021-02-02T19:10:26+05:30
[INFO] ------------------------------------------------------------------------
[INFO] Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< Database:Executor >--------------------------
[INFO] Building DatabaseExecutor 0.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ Executor ---
[INFO] Deleting C:\Users\VarunJain\Documents\Personal\DevEnv\_Setups\PracticeWorkspace\DatabaseExecutor\target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.465 s
[INFO] Finished at: 2021-02-02T19:10:28+05:30
[INFO] ------------------------------------------------------------------------
[INFO] Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< Database:Executor >--------------------------
[INFO] Building DatabaseExecutor 0.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ Executor ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\VarunJain\Documents\Personal\DevEnv\_Setups\PracticeWorkspace\DatabaseExecutor\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ Executor ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to C:\Users\VarunJain\Documents\Personal\DevEnv\_Setups\PracticeWorkspace\DatabaseExecutor\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.165 s
[INFO] Finished at: 2021-02-02T19:10:33+05:30
[INFO] ------------------------------------------------------------------------
[INFO] Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< Database:Executor >--------------------------
[INFO] Building DatabaseExecutor 0.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ Executor ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\VarunJain\Documents\Personal\DevEnv\_Setups\PracticeWorkspace\DatabaseExecutor\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ Executor ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ Executor ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\VarunJain\Documents\Personal\DevEnv\_Setups\PracticeWorkspace\DatabaseExecutor\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ Executor ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\Users\VarunJain\Documents\Personal\DevEnv\_Setups\PracticeWorkspace\DatabaseExecutor\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ Executor ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-dependency-plugin:3.1.2:copy-dependencies (copy-dependencies) @ Executor ---
[INFO] Copying log4j-api-2.14.0.jar to C:\Users\VarunJain\Documents\Personal\DevEnv\_Setups\PracticeWorkspace\DatabaseExecutor\target\libs\log4j-api-2.14.0.jar
[INFO] Copying log4j-core-2.14.0.jar to C:\Users\VarunJain\Documents\Personal\DevEnv\_Setups\PracticeWorkspace\DatabaseExecutor\target\libs\log4j-core-2.14.0.jar
[INFO] Copying junit-3.8.1.jar to C:\Users\VarunJain\Documents\Personal\DevEnv\_Setups\PracticeWorkspace\DatabaseExecutor\target\libs\junit-3.8.1.jar
[INFO] Copying spring-core-5.2.0.RELEASE.jar to C:\Users\VarunJain\Documents\Personal\DevEnv\_Setups\PracticeWorkspace\DatabaseExecutor\target\libs\spring-core-5.2.0.RELEASE.jar
[INFO] Copying spring-jcl-5.2.0.RELEASE.jar to C:\Users\VarunJain\Documents\Personal\DevEnv\_Setups\PracticeWorkspace\DatabaseExecutor\target\libs\spring-jcl-5.2.0.RELEASE.jar
[INFO] Copying spring-context-5.2.0.RELEASE.jar to C:\Users\VarunJain\Documents\Personal\DevEnv\_Setups\PracticeWorkspace\DatabaseExecutor\target\libs\spring-context-5.2.0.RELEASE.jar
[INFO] Copying spring-aop-5.2.0.RELEASE.jar to C:\Users\VarunJain\Documents\Personal\DevEnv\_Setups\PracticeWorkspace\DatabaseExecutor\target\libs\spring-aop-5.2.0.RELEASE.jar
[INFO] Copying spring-beans-5.2.0.RELEASE.jar to C:\Users\VarunJain\Documents\Personal\DevEnv\_Setups\PracticeWorkspace\DatabaseExecutor\target\libs\spring-beans-5.2.0.RELEASE.jar
[INFO] Copying spring-expression-5.2.0.RELEASE.jar to C:\Users\VarunJain\Documents\Personal\DevEnv\_Setups\PracticeWorkspace\DatabaseExecutor\target\libs\spring-expression-5.2.0.RELEASE.jar
[INFO] Copying ojdbc14-12.2.0.1.jar to C:\Users\VarunJain\Documents\Personal\DevEnv\_Setups\PracticeWorkspace\DatabaseExecutor\target\libs\ojdbc14-12.2.0.1.jar
[INFO]
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ Executor ---
[INFO] Building jar: C:\Users\VarunJain\Documents\Personal\DevEnv\_Setups\PracticeWorkspace\DatabaseExecutor\target\Executor-0.1.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.292 s
[INFO] Finished at: 2021-02-02T19:10:39+05:30
[INFO] ------------------------------------------------------------------------

虽然已成功创建可执行jar 文件,但jar 文件中缺少属性文件。我不确定这里有什么错误。有人可以指出错误或告诉我是否遗漏了什么。

提前致谢,
Varun Jain

我通过创建新项目并将内容从该项目复制到新创建的项目来自行解决。