2

I have a dependency:

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>swagger-ui</artifactId>
    <version>2.1.4</version>
</dependency>

Inside swagger-ui-2.1.4.jar has META-INF/resources/webjars/.
I would like to copy webjars/ folder to target/classes/myfolder

I've read this question and do the following:

<plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <phase>install</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <includeGroupIds>org.webjars</includeGroupIds>
                <includeArtifactIds>swagger-ui</includeArtifactIds>
                <outputDirectory>${project.build.directory}/classes/myfolder</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

But it copies the swagger-ui-2.1.4.jar to the outputDirectory, which is not specifically what I need.

Question: Is there a possible solution?

Community
  • 1
  • 1
Tran Ngu Dang
  • 2,540
  • 6
  • 29
  • 38

1 Answers1

2

You most probably don't need copy-dependencies but unpack. Check the official example for further details.

The following example:

<plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.10</version>
    <executions>
        <execution>
            <phase>install</phase>
            <goals>
                <goal>unpack</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>org.webjars</groupId>
                        <artifactId>swagger-ui</artifactId>
                        <version>2.1.4</version>
                        <type>jar</type>
                        <overWrite>false</overWrite>
                        <outputDirectory>${project.build.directory}/classes/myfolder</outputDirectory>
                        <includes>META-INF/resources/**/*.*</includes>
                    </artifactItem>
                </artifactItems>
            </configuration>
        </execution>
    </executions>
</plugin>

Will copy under ${project.build.directory}/classes/myfolder the content of META-INF/resources which is basically the webjars folder. However you would also get the META-INF/resources tree structure.


To achieve exactly your intent (copy only a subfolder content of a dependency to a certain folder) you need to use two plugins: the maven-dependency-plugin and the maven-resources-plugin as following:

<plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.10</version>
    <executions>
        <execution>
            <phase>install</phase>
            <goals>
                <goal>unpack</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>org.webjars</groupId>
                        <artifactId>swagger-ui</artifactId>
                        <version>2.1.4</version>
                        <type>jar</type>
                        <overWrite>false</overWrite>
                        <outputDirectory>${project.build.directory}/tmp</outputDirectory>
                        <includes>META-INF/resources/**/*.*</includes>
                    </artifactItem>
                </artifactItems>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.0.1</version>
    <executions>
        <execution>
            <id>copy-resources</id>
            <!-- here the phase you need -->
            <phase>install</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/classes/myfolder</outputDirectory>
                <resources>
                    <resource>
                        <directory>${project.build.directory}/tmp/META-INF/resources</directory>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

The maven-dependency-plugin would copy our desired directory tree to the target\tmp folder, then the maven-resources-plugin would copy only the subfolder we want to the final directory.

Please note both plugins will be executed during the install phase but their declaration order it really important to have the final desired outcome.

A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128