maven resources plugin flat copy of resources

2019-08-11 00:02发布

问题:

Given a folder 'database' containing JAR-connectors for different RDBMS. Each JAR is located in its own folder:

+---database
    +---db2
        +---db2.jar
    +---derby
        +---derby.jar
    +---h2
        +---h2.jar
    +---mysql
        +---mysql.jar

I need all of those JAR-files to be copied into WEB-INF\lib.

Here's my pom.xml:

<build>
    <plugins>
        ...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <webResources>
                    <resource>
                        <directory>../target/${project.artifactId}/classes/database</directory>
                        <targetPath>WEB-INF/lib</targetPath>
                        <includes>
                            <include>**/*.jar</include>
                        </includes>
                    </resource>
                </webResources>
            </configuration>
        </plugin>
    </plugins>
</build>

Problem is, that those JARs are copied with their directories:

+---WEB-INF/lib
    +---db2
        +---db2.jar
    +---derby
        +---derby.jar
    +---h2
        +---h2.jar
    +---mysql
        +---mysql.jar

This is how it should be:

+---WEB-INF/lib
    +---db2.jar
    +---derby.jar
    +---h2.jar
    +---mysql.jar

I have 20 connectors and I don't want to hard code them.

回答1:

The most correct way of doing so will be to install your jar files into Maven repository and then use maven-dependency-plugin:copy goal. Or if you want to solve this roughly then use maven-antrun-plugin (copy rule).



回答2:

You're having problems because you're trying to bend maven into something it's not supposed to do. Binary artifacts should be deployed into your artifacts repository (or local maven repository) and not included into your project. This way having them defined as dependencies in pom would ensure that they're copied into your WEB-INF/lib.