簡體   English   中英

如何在構建期間在jar外創建configs目錄

[英]How to create configs directory outside the jar during build

我的maven項目中有以下結構

src
 |-main
    |-java
       |-org.my....
          |- ..classes
    |-resources
       |-configs
          |-config1.xml
          |-config2.xml
       |-log4j.xml

我想在構建項目后獲取jar外的resources/configs目錄。 也就是說,在運行mvn clean install我想在target看到以下結構

project.jar
configs
  |-config1.xml
  |-config2.xml

我正在使用maven-shade-plugin ,我嘗試了以下內容,但它沒有用。

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
                                <manifestEntries>
                                    <Main-Class>org.mainClass</Main-Class>
                                    <Class-Path>configs</Class-Path>
                                    <!--<resource>src/main/resources/configs</resource>-->
                                    <!--<file>README.txt</file>-->
                                </manifestEntries>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>

請試試這個

https://maven.apache.org/plugins/maven-resources-plugin/examples/copy-resources.html

<outputDirectory>${basedir}/target/extra-resources</outputDirectory>

將maven資源插件添加到pom.xml

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-resources</id>
            <phase>validate</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${basedir}/configs</outputDirectory>
                <resources>
                    <resource>
                        <directory>src/main/resources/configs</directory>
                        <filtering>true</filtering>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

Doc就在這里

然后,訪問它的最佳方法是指定一個系統參數來告訴它配置目錄的位置

public class App {
    public static void main(String[] args) throws FileNotFoundException {
        InputStream inputStream = new FileInputStream(new File(System.getProperty("CONFIG_DIR") + "/config1.xml"));
        System.out.println(inputStream);
        System.out.println("Hello World!");
    }
}

然后從命令行運行它

java -DCONFIG_DIR = / home / greg / work / fat-jar / configs -jar target / fat-jar-1.0-SNAPSHOT.jar

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM