简体   繁体   English

如何使用来自另一场战争的类文件

[英]how to use class file from another war

We have two different web application and we want to extend few controllers of one war into another war. 我们有两个不同的Web应用程序,我们希望将一个战争的控制器扩展到另一个战争。

We are using maven to build the project. 我们正在使用maven来构建项目。

to include war we have given its dependency as 包括战争,我们已经将其依赖

<dependency>
            <groupId>com.abc.exchange</groupId>
            <artifactId>employer</artifactId>
            <version>2.3.M2-SNAPSHOT</version>
            <type>war</type>
            <scope>compile</scope>
        </dependency>

It is unable to build giving class not found exception. 它无法构建给定类未找到的异常。

Can any body help me out how to achieve this? 任何人都可以帮我解决这个问题吗?

I am getting error maven build failed : 我收到错误maven构建失败:

[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project agent-war: Compilation failure: Compilation failure:
[ERROR] \projects\trunk_new\agent\src\main\java\com\platform\agent\web\AgentEmployeeController.java:[22,41] cannot find symbol
[ERROR] symbol  : class EmployeeController
[ERROR] location: package com..platform.employer.web

You can define the war plugin to produce a separate jar file which is available via a classifier based on the configuration : 您可以定义war插件以生成单独的jar文件,该文件可通过基于配置的分类器获得:

<configuration>
  ..
  <attachClasses>true</attachClasses>
  <archiveClasses>true</archiveClasses>
</configuration>

After that you can use this as a separate dependency in other projects. 之后,您可以将其用作其他项目中的单独依赖项。 But the best to make a separate jar module out of it. 但最好将一个单独的jar模块从它中取出。

What you are doing is using a WAR file overlay . 你正在做的是使用WAR文件覆盖 Maven does support this. Maven确实支持这一点。 However... 然而...

The WAR plugin executes in the package phase. WAR插件在package阶段执行。 Any plugin, such as the Java compiler (which runs in the compile phase), that executes before this phase will not see any files that the WAR plugin has extracted. 在此阶段之前执行的任何插件(例如Java编译器(在编译阶段运行))都不会看到WAR插件已提取的任何文件。 (here is the reference list of all Maven phases if that helps) (如果有帮助,这里是所有Maven阶段参考列表

If you have the ability to refactor your project structure, the best way is to create a normal JAR project with your controllers and have both WARs pull this JAR in as a dependency. 如果您能够重构项目结构,最好的方法是使用控制器创建一个普通的JAR项目,并让两个WAR将此JAR作为依赖项。

If there is some reason why you can't do this, you will need to do something like: 如果有一些原因导致您无法执行此操作,则需要执行以下操作:

  1. Configure the WAR plugin to run in a phase earlier than compile , such as generate-resources and makes sure it extracts the overlay class files to ${project.build.outputDirectory} . 将WAR插件配置为在compile之前的阶段运行,例如generate-resources ,并确保将overlay类文件提取到${project.build.outputDirectory}

  2. You could also use the Maven Dependency Plugin's unpack goal to extract classes from the dependency WAR to ${project.build.outputDirectory} . 您还可以使用Maven Dependency Plugin的解包目标将依赖WAR中的类提取到${project.build.outputDirectory}

But I do recommend if at all possible to refactor your controllers into a separate JAR, it is much easier and more maintainable. 但我确实建议,如果可以将控制器重构为单独的JAR,则更容易,更易于维护。

将控制器放入一个新的jar项目,并从webprojects到这个控制器的依赖关系。

Add the following plugins to your share maven war module (for creating war and jar artifact at the same time): 将以下插件添加到您的共享maven war模块(用于同时创建war和jar工件):

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <executions>
                <execution>
                    <id>make-a-jar</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>       
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <version>2.4</version>
            <executions>
                <execution>
                    <phase>install</phase>
                    <goals>
                        <goal>install-file</goal>
                    </goals>
                    <configuration>
                        <packaging>jar</packaging>
                        <artifactId>${project.artifactId}</artifactId>
                        <groupId>${project.groupId}</groupId>
                        <version>${project.version}</version>
                        <file>
                            ${project.build.directory}/${project.artifactId}-${project.version}.jar
                        </file>
                    </configuration>
                </execution>
            </executions>
        </plugin> 

then add dependency to your dependent maven war module. 然后为依赖的maven war模块添加依赖项。

Java EE visability standards state that classes from one war shouldn't be available to classes in another war, when they're packaged as an EAR. Java EE可见性标准规定,当一个战争中的类被打包为EAR时,它们不应该可用于另一场战争中的类。 Most containers will enforce this strictly. 大多数容器都会严格执行此操作。

You should put the code you wish to share into a JAR and include that as a dependency in the war you want to use. 您应该将要共享的代码放入JAR中,并将其作为您要使用的战争中的依赖项包含在内。

In the war project pom.xml include 在战争项目中包含pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.1.1</version>
        <configuration>
            <attachClasses>true</attachClasses>
        </configuration>
</plugin>

This will create your-project-0.0.1-SNAPSHOT-clases.jar in the target folder along with the war. 这将在目标文件夹中创建your-project-0.0.1-SNAPSHOT-clases.jar以及war。

In the dependent project pom.xml include 在依赖项目中包含pom.xml

 <dependency>
        <groupId>com.yourproject</groupId>
        <artifactId>you-project</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <classifier>classes</classifier>
  </dependency>

This worked for me. 这对我有用。 Hope it helps. 希望能帮助到你。

Edit: If we want only some class files into the jar then add this to pom.xml 编辑:如果我们只想将一些类文件放入jar中,那么将其添加到pom.xml中

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
                <executions>
                    <execution>
                        <id>make-a-jar</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                            <configuration>
                                <classifier>classifier_name</classifier>
                                <includes>
                                    <include>com/../..</include>
                                </includes>
                            </configuration>
                    </execution>
                </executions>
     </plugin>

In the dependent project pom.xml include 在依赖项目中包含pom.xml

 <dependency>
        <groupId>com.yourproject</groupId>
        <artifactId>you-project</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <classifier>classifier_name</classifier>
  </dependency>

note: the classifier name must be same in both pom.xml files 注意:两个pom.xml文件中的分类器名称必须相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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