繁体   English   中英

Maven 不会向目标 jar 添加本地依赖项

[英]Maven won't add local dependency to target jar

我有一个 maven 项目,除了使用普通的 repos 外,还使用本地 jar。 jar 在清单中以这种方式定义:

    <dependency>
        <groupId>com.mirrorworlds</groupId>
        <artifactId>lstnef</artifactId>
        <version>1.0.0</version>
        <optional>false</optional>
        <scope>system</scope>
        <systemPath>${basedir}/lib/lstnef-1.0.0.jar</systemPath>
    </dependency>

安装脚本成功运行,但在应用程序启动后,我得到了这个:

Exception in thread "main" java.lang.NoClassDefFoundError: 
com/mirrorworlds/lifestreams/mail/tnef/internet/TnefMultipart 
at ...processMails(MailProcessor.java:57)
at ...main(MailReader.java:42)

当我查看目标 jar 时,我也找不到这些类,尽管它们应该在lstnef-1.0.0.jar

对于解决这个谜团的任何建议,我将不胜感激。

检查 Maven 文档: http : //maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope

system
This scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.

您需要自己手动将此 JAR 提供给运行时环境。

或者,我会推荐这种方法,设置您自己的存储库,您可以将 JARS 添加到其中并以正常的 maven 方式管理它们

使用系统范围告诉 Maven 依赖项在您提供的系统位置的 Maven“工作时间”期间可用(这与提供的范围不同,它使用正常的依赖项解析)。 之后,您必须自己“提供”文件 - 例如,将其放入 CLASSPATH(因此与提供的范围相似)。 要将文件安装到本地存储库缓存,您可以参考这篇文章:

http://maven.apache.org/plugins/maven-install-plugin/examples/specific-local-repo.html

您可以省略 localrepository 路径,maven 将安装在他的本地“缓存”中,它会在转到远程存储库之前查找所有依赖项。

当您使用 Class-Path 条目构建 manifest.mf 时(例如,当您的应用程序在 localhost 上运行时),Maven 也会支持您:要了解它是如何工作的,请阅读此处

我使用的可能解决方案是在编译阶段之前将此系统 JAR 安装到本地 Maven 存储库中,然后将此 JAR 作为 Maven 工件引用。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <version>2.2</version>
    <executions>
        <execution>
            <id>your-file</id>
            <inherited>false</inherited>
            <phase>validate</phase>
            <configuration>
                <file>${pom.basedir}/lib/your-file-4.8.jar</file>
                <repositoryLayout>default</repositoryLayout>
                <groupId>your-file</groupId>
                <artifactId>your-file</artifactId>
                <version>4.8</version>
                <packaging>jar</packaging>
                <generatePom>true</generatePom>
            </configuration>
            <goals>
                <goal>install-file</goal>
            </goals>
        </execution>
    </executions>
</plugin>

然后引用它:

<dependency>
    <groupId>your-file</groupId>
    <artifactId>your-file</artifactId>
    <version>4.8</version>    
</dependency>

你需要使用阴影插件http://maven.apache.org/plugins/maven-shade-plugin/

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.1</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <manifestEntries>
                    <Main-Class>org.sonatype.haven.ExodusCli</Main-Class>
                    <Build-Number>123</Build-Number>
                  </manifestEntries>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>

要将本地 jar 安装到本地存储库,请执行以下操作。

mvn install:install-file -Dfile=lib/ojdbc6.jar -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0.4 -Dpackaging=jar

暂无
暂无

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

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