简体   繁体   中英

How to copy resources from external jar in maven?

I have a multi-module maven based project with the following module structure:

--
 --A
   pom.xml
 --B
   pom.xml
-pom.xml

I added a dependency in the parent .pom file. Now i want to use resources from that added dependency in the A module.

Is there a way to copy only external resources to the A module, using maven?

I tried using the maven-remote-resources-plugin for that, but it doesn't see the external resources.

You have to add dependency to the module that should use it. Then you can use maven dependency plugin to get resources from that dependency.

You can define dependencies of modules A and B in parent POM under section. Then reference them in child POM (A or B) in section. This way you can make sure only a single version of given dependency is used throughout all of child modules.

All the information on dependency management in maven is documented at https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

So i found next solution. It isn't optimal but it works and does what i want. It extracts resource files from external jar (maven dependency) and copy it to class path resources.

It isn't optimal because i have to remove empty directory after files moving in needed place.

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.1.1</version>
                <executions>
                    <execution>
                        <id>unpack</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>com.group.id</groupId>
                                    <artifactId>artifact-id</artifactId>
                                    <version>${version}</version>
                                    <type>jar</type>
                                    <overWrite>false</overWrite>
                                    <includes>**/frontend/*.json</includes>
                                </artifactItem>
                            </artifactItems>
                            <outputDirectory>${project.build.directory}/classes/i18n</outputDirectory>
                            <overWriteReleases>true</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.coderplus.maven.plugins</groupId>
                <artifactId>copy-rename-maven-plugin</artifactId>
                <version>1.0</version>
                <executions>
                    <execution>
                        <id>copy-and-rename-file</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>rename</goal>
                        </goals>
                        <configuration>
                            <sourceFile>${project.build.directory}/classes/i18n/META-INF/resources/i18n/frontend
                            </sourceFile>
                            <destinationFile>${project.build.directory}/classes/i18n/frontend/
                            </destinationFile>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.8</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <delete dir="${project.build.outputDirectory}/i18n/META-INF" includeemptydirs="true"/>
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM