Maven命令行:依赖项无效

huangapple go评论59阅读模式
英文:

Maven from command line: dependencies don't work

问题

以下是翻译好的内容:

我使用了以下基本的Maven命令来生成一个项目:

mvn archetype:generate -DgroupId=it.maven.project -DartifactId=MavenExample -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false

项目已经正确创建,我可以使用以下指令无问题地测试自动生成的App类:

java -cp target/MavenExample-1.0-SNAPSHOT.jar:lib/* it.maven.project.App

后来,我向POM添加了一些依赖,得到了以下文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>it.maven.project</groupId>
  <artifactId>MavenExample</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>MavenExample</name>
  <!-- FIXME change it to the project's website -->
  <url>http://maven.apache.org</url>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

     <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.10</version>
      <scope>compile</scope>
    </dependency>

    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-collections4</artifactId>
      <version>4.0</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>

  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.7.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

最后,为了测试,我修改了之前由Maven自动生成的App类:

package it.maven.project;
import org.apache.commons.lang3.RandomStringUtils;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        System.out.println("Stringa generata casualmente: " + RandomStringUtils.random(16, true, true).toUpperCase());
    }
}

出现了一系列奇怪的情况:

  1. 尽管在POM中指定了更近版本的依赖,但在.m2文件夹中下载的版本似乎较旧(例如,我得到了commons-lang3的2.1和2.5版本)
  2. 项目通过指令 mvn clean install -U 正确编译
  3. 当我再次运行执行App的命令时,出现以下异常:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/RandomStringUtils
        at it.maven.project.App.main(App.java:13)
    Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.RandomStringUtils
        at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 1 more

问题:

  • 项目为什么可以编译,但在编译时返回错误?为什么App中的导入语句被接受,但是 System.out.println("Stringa generata casualmente: " + RandomStringUtils.random(16, true, true).toUpperCase()); 这条指令会生成异常?
  • 我应该如何更正/执行,以使我的程序能够正常工作并正确使用依赖?
英文:

I used the following basic Maven command to generate a project:

mvn archetype:generate -DgroupId=it.maven.project -DartifactId=MavenExample -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false

The project was correctly created and I could test the automatically generated App class without any problem with this instruction:

java -cp target/MavenExample-1.0-SNAPSHOT.jar:lib/* it.maven.project.App

Later on, I added some dependencies to the POM, obtaining the following file:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;

&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

  &lt;groupId&gt;it.maven.project&lt;/groupId&gt;
  &lt;artifactId&gt;MavenExample&lt;/artifactId&gt;
  &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;

  &lt;name&gt;MavenExample&lt;/name&gt;
  &lt;!-- FIXME change it to the project&#39;s website --&gt;
  &lt;url&gt;http://maven.apache.org&lt;/url&gt;

  &lt;dependencies&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;junit&lt;/groupId&gt;
      &lt;artifactId&gt;junit&lt;/artifactId&gt;
      &lt;version&gt;4.11&lt;/version&gt;
      &lt;scope&gt;test&lt;/scope&gt;
    &lt;/dependency&gt;

     &lt;dependency&gt;
      &lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
      &lt;artifactId&gt;commons-lang3&lt;/artifactId&gt;
      &lt;version&gt;3.10&lt;/version&gt;
      &lt;scope&gt;compile&lt;/scope&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
      &lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
      &lt;artifactId&gt;commons-collections4&lt;/artifactId&gt;
      &lt;version&gt;4.0&lt;/version&gt;
      &lt;scope&gt;compile&lt;/scope&gt;
    &lt;/dependency&gt;
  &lt;/dependencies&gt;

  &lt;build&gt;
    &lt;sourceDirectory&gt;src&lt;/sourceDirectory&gt;
    &lt;plugins&gt;
      &lt;plugin&gt;
        &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
        &lt;version&gt;3.7.0&lt;/version&gt;
        &lt;configuration&gt;
          &lt;source&gt;1.8&lt;/source&gt;
          &lt;target&gt;1.8&lt;/target&gt;
        &lt;/configuration&gt;
      &lt;/plugin&gt;
    &lt;/plugins&gt;
  &lt;/build&gt;



&lt;/project&gt;

Finally, for testing sake, I modified the App class previously generated by Maven itself:

package it.maven.project;
import org.apache.commons.lang3.RandomStringUtils;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( &quot;Hello World!&quot; );
        System.out.println(&quot;Stringa generata casualmente: &quot; + RandomStringUtils.random(16, true, true).toUpperCase());
    }
}

A series of strange things happen:

  1. even though more recent versions of dependencies are specified in POM, in .m2 folder the downloaded versions seem to be older (for instance I get 2.1 and 2.5 for commons-lang3)

  2. the project is correctly compiled by instruction

    mvn clean install -U

  3. when I run again command to execute App, I get the following exception:

> Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/RandomStringUtils
at it.maven.project.App.main(App.java:13)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.RandomStringUtils
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more

Questions:

  • how is it possible that project compiles but then errors are returned at compiling time? Why the import in App is accepted but instruction:

    System.out.println(&quot;Stringa generata casualmente: &quot; + RandomStringUtils.random(16, true, true).toUpperCase());

    generates an exception?

  • what should I correct/execute to allow my program to work and use correctly dependencies?

答案1

得分: 0

以下是您要翻译的内容:

当您创建一个简单的Java项目时,您会在pom中定义一组依赖关系,但是如果您不创建Java存档文件(ejb-jar或war),则在运行目标目录中编译的jar时,所有依赖关系在运行时不可用。
有两种解决方案:

  1. 创建一个超级jar,将所有依赖项包含在您的jar中:使用stackoverflow的解决方案

  2. 当您从命令行运行jar时,您必须将依赖项添加到类路径中:

    java -cp "/path/dependencies/dep1.jar;/path/dependencies/dep2.jar" -jar myApp.jar

英文:

when you create a simple java project you define a set of dependencies in the pom but if you don't create a java archive file (ejb-jar or war) all the dependencies are not available at runtime if you run the jar compiled in the target directory.
There are two solutions:

  1. Create a uber jar that include all your depenencies in your jar: use stackoverflow solution

  2. when you run the jar from command line you have to add the dependencies to the classpath:

    java -cp "/path/dependencies/dep1.jar;/path/dependencies/dep2.jar" -jar myApp.jar

huangapple
  • 本文由 发表于 2020年10月13日 04:25:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/64324904.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定