Java 如何在使用 maven 构建的战争中包含系统依赖项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19065666/
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-08-12 13:49:14  来源:igfitidea点击:

How to include system dependencies in war built using maven

javaeclipsemaven

提问by coding_idiot

I've searched on internet for quite some time and I'm unable to figure out how to configure the maven-war plugin or something alike so that the system dependencies are included in the built-war (WEB-INF/lib folder)

我已经在互联网上搜索了很长一段时间,但我无法弄清楚如何配置 maven-war 插件或类似的东西,以便系统依赖项包含在内置战争(WEB-INF/lib 文件夹)中

I use the maven dependency plugin in case of a jar-build as :

在 jar-build 的情况下,我使用 maven 依赖插件:

<plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <phase>install</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

but I'm unable to understand what is to be done in case of a war build. I've tried using the maven-war plugin, but it's not including system-dependencies in the build.

但我无法理解在战争构建的情况下要做什么。我试过使用 maven-war 插件,但它没有在构建中包含系统依赖项。

[UPDATE]

[更新]

I'm having depedencies of type :

我有类型的依赖:

<dependency>
    <groupId>LoginRadius</groupId>
    <artifactId>LoginRadius</artifactId>
    <scope>system</scope>
    <version>1.0</version>
    <systemPath>${basedir}\lib\LoginRadius-1.0.jar</systemPath>
</dependency>

in my POM and these dependencies are not included in WEB-INF/lib when the war is build.

在我的 POM 中,并且在构建战争时这些依赖项不包含在 WEB-INF/lib 中。

采纳答案by coding_idiot

Let me try to summarise the options I tried :

让我尝试总结一下我尝试过的选项:

<packagingIncludes>${java.home}/lib/jfxrt.jar</packagingIncludes>

This doesn't work! Also, only having the jar name, excludes everything else, so if you are willing to try then try

这不起作用!此外,只有 jar 名称,排除了其他所有内容,因此如果您愿意尝试,请尝试

<packagingIncludes>${java.home}/lib/jfxrt.jar,**/*</packagingIncludes>

Jatin's answer seemed a bit complex and I tried going through the POM again & again to figure out where exactly were the system jars mentioned to be included in WEB-INF POM.

Jatin 的回答似乎有点复杂,我尝试一遍又一遍地浏览 POM,以找出提到的系统 jar 确切地包含在 WEB-INF POM 中的位置。

Anyways, I ended up using this solution, which wasn't working at first but after some time and some corrections worked :

无论如何,我最终使用了这个解决方案,它起初不起作用,但经过一段时间和一些更正后起作用:

I installed the jar in my local repository using the below command :

我使用以下命令在本地存储库中安装了 jar:

mvn install:install-file -Dfile="C:\Users\hp\Documents\NetBeansProjects\TwitterAndLoginRadiusMaven\lib\LoginRadius-1.0.jar" -DgroupId=LoginRadius -DartifactId=LoginRadius -Dversion=1.0 -Dpackaging=jar`

After running the above command, I changed the dependency in POM to

运行上述命令后,我将POM中的依赖更改为

<dependency>
   <groupId>LoginRadius</groupId>
   <artifactId>LoginRadius</artifactId>
   <!--<scope>system</scope>-->
   <version>1.0</version>
   <!--<systemPath>${basedir}\lib\LoginRadius-1.0.jar</systemPath>-->
</dependency>

NOTE - See I've commented the system scope & systemPath.

注意 - 请参阅我对系统范围和系统路径的评论。

Building the war now, includes this LoginRadius-1.0.jar in WEB-INF/lib

现在构建战争,在 WEB-INF/lib 中包含这个 LoginRadius-1.0.jar

回答by Jatin

If you meant as jar dependencies, then below is a sample pom.xml which takes are needed files and generates a war file:

如果您的意思是 jar 依赖项,那么下面是一个示例 pom.xml,它接受所需的文件并生成一个 war 文件:

 <build>
        <defaultGoal>install</defaultGoal>
        <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
        <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
        <outputDirectory>${project.basedir}/target/classes</outputDirectory>
        <testOutputDirectory>${project.basedir}/target/test-classes</testOutputDirectory>
        <resources>
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
            </resource>
        </resources>
        <testResources>
            <testResource>
                <directory>${project.basedir}/src/test/resources</directory>
            </testResource>
        </testResources>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.0.2</version>
                    <configuration>
                        <source>1.5</source>
                        <target>1.5</target>
                        <debug>true</debug>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.4</version>
                    <configuration>
                        <includeEmptyDirs>true</includeEmptyDirs>
                        <webResources>
                            <resource>
                                <directory>ui</directory>
                                <targetPath></targetPath>
                                <includes>
                                    <include>**</include>
                                </includes>
                            </resource>
                            <resource>
                                <directory>lib</directory>
                                <targetPath>WEB-INF</targetPath>
                                <includes>
                                    <include>**/*.xml</include>
                                    <include>**/log4j.properties</include>
                                </includes>
                            </resource>
//edited below
                            <resource>
                               <directory>lib</directory>
                               <targetPath>WEB_INF/lib</targetPath>
                               <includes>
                                    <include>**/*.jar</include>
                               </includes>
                            </resource>
                        </webResources>
                        <webXml>${project.basedir}/WEB-INF/web.xml</webXml>
                    </configuration>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-source-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>attach-sources</id>
                            <goals>
                                <goal>jar</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

回答by jdevora

Based on your initial POM, I would suggest to send them directly to the WEB-INF/lib directory

根据您的初始 POM,我建议将它们直接发送到 WEB-INF/lib 目录

                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/${project.artifactId}-${project.version}/WEB-INF/lib</outputDirectory>
                    </configuration>

回答by JRSofty

If by chance you can't install the third party library to your local repository, due to some silly naming/packaging checks by the third party, you can still add your systemscoped dependencies to your final package at build time (at least if you are building a webapp) using the maven-war-pluginwhere you would need to create a configuration like this.

如果您偶然无法将第三方库安装到本地存储库,由于第三方进行了一些愚蠢的命名/打包检查,您仍然可以system在构建时将范围依赖项添加到最终包中(至少如果您是构建一个 webapp) 使用maven-war-plugin您需要创建这样的配置的地方。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.2.0</version>
    <configuration>
        <failOnMissingWebXml>true</failOnMissingWebXml>
        <webResources>
            <resource>
               <directory>path/to/lib/in/project</directory>
               <targetPath>WEB-INF/lib</targetPath>
            </resource>
        </webResources>
    </configuration>
</plugin>

Not sure but I believe that the library must be somewhere local to the project's base directory. I tend to create a directory under src/main/called libto hold these sorts of 3rd party libs. During build process they are placed in the correct directory and added to the war file.

不确定,但我相信该库必须位于项目基本目录的本地位置。我倾向于在src/main/调用下创建一个目录lib来保存这些类型的 3rd 方库。在构建过程中,它们被放置在正确的目录中并添加到 war 文件中。

回答by Tiago Medici

mvn org.apache.maven.plugins:maven-install-plugin:2.5.1:install-file \
  -Dfile=~/Desktop/medici-1.0.1.jar \
  -DgroupId=com.hp \
  -DartifactId=medici \
  -Dversion=1.0.1 \
  -Dpackaging=jar \
  -DlocalRepositoryPath=lib

If all went well, you can find your artifact published inside of lib.

如果一切顺利,您可以在 lib 中找到发布的工件。

project$ find lib
lib
lib/com
lib/com/hp
lib/com/hp/medici
lib/com/hp/medici/1.0.1
lib/com/hp/medici/1.0.1/medici-1.0.1.jar
lib/com/hp/medici/1.0.1/medici-1.0.1.pom
lib/com/hp/medici/maven-metadata-local.xml
Note the structure here mimics what you'd find in ~/.m2.

Now, in your pom.xml, declare it in your project a Maven repository.

现在,在您的 pom.xml 中,在您的项目中将其声明为 Maven 存储库。

<repositories>
    <repository>
        <id>local-repo</id>
        <url>file://${basedir}/lib</url>
    </repository>
</repositories>

And lastly, in your pom.xml declare a dependency on the local .jar like you would for any dependency.

最后,在你的 pom.xml 中声明对本地 .jar 的依赖,就像你对任何依赖一样。

<dependencies>
    <dependency>
        <groupId>com.hp.medici</groupId>
        <artifactId>medici</artifactId>
        <version>1.0.1</version>
    </dependency>
</dependencies>

回答by tremendows

It may be problem of the scope where the dependencies are defined:

可能是定义依赖项的范围的问题:

  • compile: Dependencies in "compile" scope will automatically be copied to the target's WEB-INF/lib as part of the Maven build.
  • provided: Dependencies in "provided" scope will not be copied (you would use this for server-supplied jars such as the JSP-api.jar).
  • compile:“compile”范围内的依赖项将作为 Maven 构建的一部分自动复制到目标的 WEB-INF/lib。
  • 提供:“提供”范围内的依赖项将不会被复制(您可以将它用于服务器提供的 jar,例如 JSP-api.jar)。

To generate a WAR in Maven, 2 goals have to be run: "compile" and "war:war". Running the war goal alone won't compile the Java source . If you prefer, run one of the master goals, such as "package".

要在 Maven 中生成 WAR,必须运行 2 个目标:“compile”和“war:war”。单独运行 war 目标不会编译 Java 源代码。如果您愿意,可以运行其中一个主要目标,例如“包”。

If using eclipse:

如果使用日食

select the POM in the project explorer and select the "Run" context menu option. This will build and execute Maven run profile (or use an existing one). Maven run profiles behave just like regular run/debug profiles except that their profile edit dialogs support Maven-specific features.

在项目资源管理器中选择 POM,然后选择“运行”上下文菜单选项。这将构建并执行 Maven 运行配置文件(或使用现有的配置文件)。Maven 运行配置文件的行为就像常规的运行/调试配置文件一样,只是它们的配置文件编辑对话框支持 Maven 特定的功能。

source: https://coderanch.com/t/594897/ide/include-maven-dependencies-war-file

来源:https: //coderanch.com/t/594897/ide/include-maven-dependencies-war-file

回答by Ankit Nigam

You can configure the war plugin to have allor somejar includedor excludedas per your need as mentioned below. Simple & works

您可以根据需要将 war 插件配置为包含排除全部部分jar ,如下所述。简单而有效

           <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <webResources>
                        <resource>
                            <directory>${project.basedir}\lib</directory>
                            <targetPath>WEB-INF/lib</targetPath>
                            <filtering>false</filtering>
                            <includes>
                                <include>**/*.jar</include>
                            </includes>
                            <excludes>
                                <include>**/javax.servlet-api-3.1.0.jar</include>
                            </excludes>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>