Artyom Karnov
Artyom Karnov

Reputation: 597

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.

Upvotes: 2

Views: 4476

Answers (3)

Artyom Karnov
Artyom Karnov

Reputation: 597

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> 

Upvotes: 1

Chaveen Ekanayake
Chaveen Ekanayake

Reputation: 11

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

Upvotes: 0

Martin Špelina
Martin Špelina

Reputation: 51

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.

Upvotes: 0

Related Questions