java 如何使用另一场战争中的类文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10188495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:59:14  来源:igfitidea点击:

how to use class file from another war

javamavenmaven-pluginmaven-3

提问by Zuned Ahmed

We have two different web application and we want to extend few controllers of one war into another war.

我们有两个不同的 Web 应用程序,我们想将一场War的几个控制器扩展到另一场War。

We are using maven to build the project.

我们正在使用 maven 来构建项目。

to include war we have given its dependency as

将War包括在内,我们将其依赖作为

<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 build failed :

[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

回答by khmarbaise

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 模块。

回答by prunge

What you are doing is using a WAR file overlay. Maven does support this. However...

您正在做的是使用WAR 文件覆盖。Maven 确实支持这一点。然而...

The WAR plugin executes in the packagephase. 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. (here is the reference list of all Maven phasesif that helps)

WAR 插件在package阶段中执行。在此阶段之前执行的任何插件,例如 Java 编译器(在编译阶段运行),都不会看到 WAR 插件已提取的任何文件。(如果有帮助,这里是所有 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-resourcesand makes sure it extracts the overlay class files to ${project.build.outputDirectory}.

  2. You could also use the Maven Dependency Plugin's unpackgoal to extract classes from the dependency WAR to ${project.build.outputDirectory}.

  1. 将 WAR 插件配置为在早于 的阶段运行compile,例如generate-resources并确保它将覆盖类文件提取到${project.build.outputDirectory}.

  2. 您还可以使用 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 中,它更容易且更易于维护。

回答by ollins

put the controllers into a new jar project and make dependencies from your webprojects to this controllers.

将控制器放入一个新的 jar 项目中,并使您的 web 项目依赖于此控制器。

回答by Saeed Zarinfam

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模块。

回答by Ruthwik

In the war project pom.xml include

在War项目 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.

这将与War一起在目标文件夹中创建 your-project-0.0.1-SNAPSHOT-clases.jar。

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 文件中的分类器名称必须相同

回答by Deco

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. Most containers will enforce this strictly.

Java EE 可见性标准规定,当一场War中的类被打包为 EAR 时,另一场War中的类不应该可用。大多数容器会严格执行这一点。

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 中,并将其作为依赖项包含在您要使用的War中。