简体   繁体   中英

JAR under EAR(src/main/application/lib) are not part of ClassPath in Maven

I am working on converting a J2EE application to Maven where the EAR project will contain a WAR module. I have followed the below URL to convert the project and it does work with some minor changes: https://www.ibm.com/docs/en/wasdtfe?topic=projects-converting-existing-maven

In the current project, there are some libraries under the EAR folder which I cannot move to the local maven repository . The reason is old legacy code which expects these library names to be intact (myCommon.jar and no version to be added like myCommon-1.0.jar). As a workaround , I placed these libs under EAR->src->main->application->lib folder . There is no build failure observed but the major problem is with the ClassPath for these EAR lib files as shown below:

[err] java.lang.ClassNotFoundException: com.myClass.classFromWAR
[err] at java.lang.Class.forNameImpl(Native Method)
[err] at java.lang.Class.forName(Class.java:332)

Eg myCommon.jar contains code like the below:

public void EARLibFunc( string classNameFromWAR){
    .........
    //E.g. classNameFromWAR = "com.myClass.classFromWAR";
    final Class warClass = Class.forName( classNameFromWAR );
        .........
}

Calling above function from the java files inside WAR module reports ClassNotFoundException: EARLibFunc("com.myClass.classFromWAR");


The directory structure looks like the below:

WARProject
-src
----com
--------myClass
------------classFromWAR.java

EARProject
-src
----main
--------application
------------lib
----------------myCommon.jar

The jar files from EAR/src/main/application don't seem to be part of the ClassPath.

Can you please suggest the best practice to handle such an issue? What should be the correct layout of the EAR libraries to make it part of the ClassPath? Please be informed that the code from the EAR libraries cannot be changed (legacy code dependency issue).

For reference here are my pom settings:

WARProject pom.xml:

.......
.......
    <groupId>MyApp</groupId>
    <artifactId>MyApp</artifactId>
    <version>3.5</version>
    <packaging>war</packaging>
    <description>MyApp Maven</description>
........
    <build>
        <resources>
            <resource>
                <directory>Java Source</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <warSourceDirectory>Web Content</warSourceDirectory>
                </configuration>
            </plugin>
        </plugins>
    </build>    
......

EAR Project pom.xml (contains WAR module as dependency):

.....
    <groupId>EARProject_EAR</groupId>
    <artifactId>EARProject_EAR</artifactId>
    <version>3.5</version>
    <packaging>ear</packaging>
    <description>My Project EAR</description>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-ear-plugin</artifactId>
                <version>2.10</version>
                <configuration>
                    <version>7</version>
                    <skinnyWars>true</skinnyWars>
                    <defaultLibBundleDir>lib</defaultLibBundleDir>
                    <archive>
                        <manifest>
                            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                            <addClasspath>true</addClasspath>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>MyApp</groupId>
            <artifactId>MyApp</artifactId>
            <version>3.5</version>
            <type>war</type>
        </dependency>
    </dependencies>
......

I think, it should be possible this way:

  1. Install the jar in your local maven repository.
  2. Configure the maven-ear-plugin to include third party libraries as shown here .
  3. Add <bundleFileName>myCommon.jar</bundleFileName> to jarModule in order to give your JAR file the desired name within the EAR.
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-ear-plugin</artifactId>
            <version>3.2.0</version>
            <configuration>
               [...]
               <modules>
                 <jarModule>
                   <groupId>artifactGroupId</groupId>
                   <artifactId>artifactId</artifactId>
                   <includeInApplicationXml>true</includeInApplicationXml>
                   <bundleFileName>myCommon.jar</bundleFileName>
                 </jarModule>
              </modules>
            </configuration>
          </plugin>
        </plugins>
      </build>

More information can be found at the usage page of the plugin.

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