S.Dan
S.Dan

Reputation: 1912

How to create configs directory outside the jar during build

I have the following structure in my maven project

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

I want to get the resources/configs directory outside the jar after building the project. That is, after I run mvn clean install I want to see the following structure inside the target

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

I'm using maven-shade-plugin and I tried out the following, but it didn't work.

    <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>

Upvotes: 3

Views: 1922

Answers (2)

Essex Boy
Essex Boy

Reputation: 7950

Add the maven resources plugin to you 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 is here

The best way to access it is then to specify a system parameter to tell it where the config directory is

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!");
    }
}

Then run it from the command line

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

Upvotes: 2

Chanaka Lakmal
Chanaka Lakmal

Reputation: 1122

Please try with this

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

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

Upvotes: 2

Related Questions