【问题标题】:Maven: can not package local jar into final jar with maven and the error is java.lang.NoClassDefFoundErrorMaven:无法使用maven将本地jar打包成最终jar,错误为java.lang.NoClassDefFoundError
【发布时间】:2019-12-10 01:42:05
【问题描述】:

将本地jar复制到/src/main/resources/,并添加Maven依赖为How to add local jar files to a Maven project?

        <dependency>
            <groupId>com.yahoo.egads</groupId>
            <artifactId>egads</artifactId>
            <version>0.4.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/egads-0.4.0-jar-with-dependencies.jar</systemPath>
        </dependency>

插件如下:

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

本地jar类可以在IntelliJ IDEA中定位,主函数可以在IntelliJ IDEA按钮run中运行成功。但是运行最终打包的jar时出错,logjava.lang.NoClassDefFoundError找不到本地jar的类。

打包时有如下警告:

'dependencies.dependency.systemPath' for com.yahoo.egads:egads:jar should not point at files within the project directory, ${project.basedir}/src/main/resources/egads-0.4.0-jar-with-dependencies.jar will be unresolvable by dependent projects @ line 36, column 25

【问题讨论】:

标签: java scala maven


【解决方案1】:

如何将本地 jar 文件添加到 Maven 项目中?

这个视频解决了我的问题。

https://www.youtube.com/watch?v=3nGwbRONhEU

使用 Netbeans,就像添加依赖项一样简单(在您的情况下是这样): 在您的“pom.xml”文件中: ... com.yahoo.egads 埃加兹 0.4.0 ... 然后打开依赖文件夹;

  1. 右键单击“egads-0.4.0.jar”
  2. 然后点击手动安装工件
  3. 搜索您的 .jar 文件并选择它
  4. 最后,就在您的项目上并“清理并构建” 我

执行“清理并构建”后如果出现以下错误:

Plugin org.apache.maven.plugins:maven-clean-plugin:2.4.1 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-clean-plugin:jar:2.4.1: Could not transfer artifact org.apache.maven.plugins:maven-clean-plugin:pom:2.4.1 from/to central (http://repo.maven.apache.org/maven2): Failed to transfer file: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.4.1/maven-clean-plugin-2.4.1.pom. Return code is: 501 , ReasonPhrase:HTTPS Required. -> [Help 1]

To see the full stack trace of the errors, re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.

For more information about the errors and possible solutions, please read the following articles:
[Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginResolutionException

然后以下内容对我来说 100% 有效: (感谢参考:Maven dependencies are failing with a 501 error

添加你的“pom.xml”文件(在项目设置中,只要你创建了一个maven java项目):

<project>
      ...
    <pluginRepositories>
        <pluginRepository>
            <id>central</id>
            <name>Central Repository</name>
            <url>https://repo.maven.apache.org/maven2</url>
            <layout>default</layout>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <releases>
                <updatePolicy>never</updatePolicy>
            </releases>
        </pluginRepository>
    </pluginRepositories>
    <repositories>
        <repository>
            <id>central</id>
            <name>Central Repository</name>
            <url>https://repo.maven.apache.org/maven2</url>
            <layout>default</layout>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>

然后,右键单击您的项目,按 -> Clean and Build。 如果“成功”,就是这样。 否则...(更多错误)尝试(我可以推荐的最后一件事):

放入你的“settings.xml”文件(在你的项目设置中,靠近 pom.xml),如下:

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
  <localRepository>${user.home}/.m2/repository</localRepository>
  <interactiveMode>true</interactiveMode>
  <offline>false</offline>
</settings>




















不能保证以下方法会起作用:

(尝试冒着失去时间和耐心的风险..)

如果这不起作用并且您仍然有错误,我只能说我以前做过的其他事情,但不能保证它们会起作用(在这里):

https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html

删除所有“”并在命令行中使用您的自定义参数执行该命令。

我在 ~/.m2/repository 中执行了该操作 (/.m2/repository)

希望它对你有用。 我今天花了太多时间试图解决这个问题。关于stackoverflow的第一篇关于这个的帖子充满了令人困惑的事情,解释得不好,至少对我来说不起作用。不过还好。 我会在那里发表评论,但我没有得分。

原始帖子(非常混乱/不好解释): How to add local jar files to a Maven project?

我的“pom.xml”中也有以下内容(在尝试了很多我不记得的东西之后)

在“”中写入您的 Java 源代码包(您的 .java 类的父级):

    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <configuration>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
            <archive>
                <manifest>
                    <mainClass>com.mycompany.app</mainClass> 
                </manifest>
            </archive>
        </configuration>
        <executions>
            <execution>
                <id>make-assembly</id>
                    <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
            </execution>
        </executions>
    </plugin>

最后,我的“settings.xml”文件中也有这个:

    <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
      <localRepository>${user.home}/.m2/repository</localRepository>
      <interactiveMode>true</interactiveMode>
      <offline>false</offline>
    </settings>

(我把它放在最后一部分,因为可能第一件事它会起作用,但如果不是你不能尝试做我在最后一部分所做的一切,以防万一)

另外,所有对我有用的链接是:

维基:

https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

https://techglimpse.com/create-maven-jar-file-netbeans-tutorial/

http://maven.apache.org/general.html#importing-jars (我只记得直截了当地说“有一个我想放入本地存储库的 jar。我该如何复制它?”)

https://www.youtube.com/watch?v=3nGwbRONhEU

(我没有放无用链接的文档)

【讨论】:

    猜你喜欢
    • 2019-01-23
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2020-01-08
    • 1970-01-01
    相关资源
    最近更新 更多