简体   繁体   中英

Bind a file inside a maven built EAR

Below is my project structure. 在此处输入图片说明

I require to copy a file from the location \\MainProject\\src\\non-packaged-resources to the \\MainProject\\sub-project2\\sub-project2-web\\src\\main\\resources location on building the MainProject. Below is the build section of the pom file of my sub-project2-web but the file is not copied while building.

    <build>
    <finalName>sub-project2-web</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <warSourceExcludes>**/.copyarea.db</warSourceExcludes>
                <packagingExcludes>**/.copyarea.db</packagingExcludes>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.5</version>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <!-- here the phase you need -->
                    <phase>validate</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>/src/main/resources</outputDirectory>
                        <resources>
                            <resource>
                                <directory>/src/non-packaged-resources</directory>
                                <filtering>true</filtering>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

When observing the build log, it skips the file being copied with the below error

skip non existing resourceDirectory

But the maven build gets successful. Goal is to bind the file which exists inside the \\MainProject\\src\\non-packaged-resources to all the ears (sub-project1.ear, sub-project2.ear) on building the maven project. First I am trying to test this in the sub-project2-web only. Please advice on how to provide correct paths based on this requirement. I am using maven 2.2.1 version (project is built on this.)

Define the paths with reference to your project base directory as follows.

<outputDirectory>${project.basedir}/sub-project2/sub-project2-web/src/main/resources</outputDirectory>

<directory>${project.basedir}/src/non-packaged-resources</directory>

Then add it in the pom.xml file in the maven-resources-plugin .

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.5</version>
    <executions>
        <execution>
            <id>copy-resources</id>
            <!-- here the phase you need -->
            <phase>validate</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.basedir}/sub-project2/sub-project2-web/src/main/resources</outputDirectory>
                <resources>
                    <resource>
                        <directory>${project.basedir}/src/non-packaged-resources</directory>
                        <filtering>true</filtering>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

Based on the above answer I found the correct way to get the locations of the parent and the base directories. Below worked for me.

<build>
<finalName>sub-project2-web</finalName>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <configuration>
            <warSourceExcludes>**/.copyarea.db</warSourceExcludes>
            <packagingExcludes>**/.copyarea.db</packagingExcludes>
        </configuration>
    </plugin>
    <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.5</version>
        <executions>
            <execution>
                <id>copy-resources</id>
                <!-- here the phase you need -->
                <phase>validate</phase>
                <goals>
                    <goal>copy-resources</goal>
                </goals>
                <configuration>                         
                    <outputDirectory>${project.basedir}/src/main/resources</outputDirectory>
                    <resources>
                        <resource>                      
                            <directory>${project.parent.parent.basedir}/src/non-packaged-resources</directory>
                            <filtering>true</filtering>
                        </resource>
                    </resources>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>

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