Install local jar with Maven – 用Maven安装本地jar

最后修改: 2013年 4月 17日

中文/混合/英文(键盘快捷键:t)

1. The Problem and the Options

1.问题和选择

Maven is a very versatile tool and its available public repositories are second to none. However there will always be an artifact that is either not hosted anywhere, or the repository where it is hosted is risky to depend on, as it may not be up when you need it.

Maven是一个非常通用的工具,其可用的公共资源库首屈一指。然而,总有一些工件要么不在任何地方托管,要么托管该工件的仓库有风险,因为当你需要它时,它可能不会出现。

When that happens, there are a few choices:

当这种情况发生时,有几个选择。

  • bite the bullet and install a full fledged repository management solution such as Nexus
  • try to get the artifact uploaded into one of more reputable public repositories
  • install the artifact locally using a maven plugin

Nexus is of course the more mature solution, but it’s also the more complex. Provisioning an instance to run Nexus, setting up Nexus itself, configuring and maintaining it may be overkill for such a simple problem as using a single jar. If this scenario – hosting custom artifacts – is a common one however, a repository manager makes a lot of sense.

Nexus当然是更成熟的解决方案,但它也是更复杂的。对于使用单个jar这样一个简单的问题来说,配置一个实例来运行Nexus、设置Nexus本身、配置和维护Nexus可能是过犹不及。如果这种情况–托管自定义工件–是一种常见的情况,那么资源库管理器就很有意义了。

Getting the artifact uploaded into a public repository or in Maven central directly is also a good solution, but usually a lengthy one. In addition, the library may not be Maven enabled at all, which makes the process that much more difficult, so it’s not a realistic solution to being able to use the artifact NOW.

将工件上传到公共仓库或直接上传到Maven中心也是一个不错的解决方案,但通常一个冗长的过程。此外,该库可能根本没有启用Maven,这使得这一过程更加困难,所以这不是一个能够在现在使用工件的现实解决方案。

That leaves the third option – adding the artifact in source control and using a maven plugin – in this case, the maven-install-plugin to install it locally before the build process needs it. This is by far the easiest and most reliable option available.

这就留下了第三种选择–在源码控制中添加工件并使用maven插件–在这种情况下,maven-install-plugin,在构建过程需要之前将其安装到本地。这是迄今为止最简单、最可靠的选择。

2. Install Local Jar With the maven-install-plugin

2.用maven-install-plugin安装本地Jar

Let’s start with the full configuration necessary to install the artifact into our local repository:

让我们从将工件安装到我们的本地仓库所需的全部配置开始。

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-install-plugin</artifactId>
   <version>2.5.1</version>
   <configuration>
      <groupId>org.somegroup</groupId>
      <artifactId>someartifact</artifactId>
      <version>1.0</version>
      <packaging>jar</packaging>
      <file>${basedir}/dependencies/someartifact-1.0.jar</file>
      <generatePom>true</generatePom>
   </configuration>
   <executions>
      <execution>
         <id>install-jar-lib</id>
         <goals>
            <goal>install-file</goal>
         </goals>
         <phase>validate</phase>
      </execution>
   </executions>
</plugin>

Now, let’s break down and analyze the details of this configuration.

现在,让我们来分解和分析一下这个配置的细节。

2.1. The Artifact Information

2.1.工艺品信息

The Artifact Information is defined as part of the <configuration> element. The actual syntax is very similar to declaring the dependency – a groupId, artifactId and version elements.

Artifact信息被定义为<配置>元素的一部分。实际的语法与声明依赖关系非常相似–一个groupIdartifactIdversion元素。

Next part of the configuration requires defining the packaging of the artifact – this is specified as jar.

配置的下一部分需要定义工件的packaging – 这被指定为jar

Next, we need to provide the location of the actual jar file to be installed – this can be an absolute file path or it can be relative, using the properties available in Maven. In this case, the ${basedir} property represents the root of the project, namely the location where the pom.xml file exists. This means that the someartifact-1.0.jar file needs to be placed in a /dependencies/ directory under the root.

接下来,我们需要提供要安装的实际jar文件的location–这可以是一个绝对文件路径,也可以是相对路径,使用Maven中可用的属性。在这种情况下,${basedir}属性代表项目的根,即pom.xml文件所在的位置。这意味着someartifact-1.0.jar文件需要放在根目录下的/dependencies/目录中。

Finally, there are several other optional details that can be configured as well.

最后,还有其他几个可选的细节,也可以进行配置。

2.2. The Execution

2.2.执行

The execution of the install-file goal is bound to the validate phase from the standard Maven build lifecycle. As such, before attempting to compile – you will need to run the validation phase explicitly:

install-file目标的执行与标准Maven 构建生命周期中的validate阶段绑定。因此,在尝试编译之前–您需要明确运行验证阶段。

mvn validate

After this step, the standard compilation will work:

在这一步之后,标准的编译就可以工作了。

mvn clean install

Once the compile phase does execute, our someartifact-1.0.jar is correctly installed in our local repository, just as any other artifact that may have been retrieved from Maven central itself.

一旦编译阶段执行,我们的someartifact-1.0.jar就会正确安装在本地仓库中,就像其他可能从Maven中心获取的工件一样。

2.3. Generating a POM vs Supplying the POM

2.3.生成POM与提供POM

The question of whether we need to supply a pom.xml file for the artifact or not depends on mainly on the runtime dependencies of the artifact itself. Simply put, if the artifact has runtime dependencies on other jars, these jars will need to be present on the classpath at runtime as well. With a simple artifact that should not be a problem, as it will likely have no dependencies at runtime (a leaf in the dependency graph).

我们是否需要为工件提供一个pom.xml文件的问题主要取决于工件本身的运行时依赖性。简单地说,如果工件在运行时依赖其他jar,这些jar在运行时也需要在classpath上出现。对于一个简单的工件来说,这不应该是一个问题,因为它在运行时可能没有依赖关系(依赖关系图中的一个叶子)。

The generatePom option in the install-file goal should suffice for these kinds of artifacts:

install-file目标中的generatePom选项应该足以满足这些类型的工件。

<generatePom>true</generatePom>

However, if the artifact is more complex and does have non-trivial dependencies, then, if these dependencies are not already in the classpath, they must be added. One way to do that is by defining these new dependencies manually in the pom file of the project. A better solution is to provide a custom pom.xml file along with the installed artifact:

但是,如果工件比较复杂,并且确实有非微不足道的依赖性,那么,如果这些依赖性还没有在classpath中,就必须将它们加入。一种方法是在项目的pom文件中手动定义这些新的依赖项。一个更好的解决方案是提供一个自定义的pom.xml文件与安装的工件一起。

<generatePom>false</generatePom>
<pomFile>${basedir}/dependencies/someartifact-1.0.pom</pomFile>

This will allow Maven to resolve all dependencies of the artifact defined in this custom pom.xml, without having to define them manually in the main pom file of the project.

这将使Maven能够解决该自定义pom.xml中定义的工件的所有依赖关系,而不需要在项目的主pom文件中手动定义它们。

3. Conclusion

3.结论

This article goes over how to use a jar which is not hosted anywhere within a Maven project by installing it locally with the maven-install-plugin.

本文介绍了如何通过使用maven-install-plugin在本地安装未在Maven项目中托管的jar。