Maven命令行:依赖项无效

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

Maven from command line: dependencies don't work

问题

以下是翻译好的内容:

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

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

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

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

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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>it.maven.project</groupId>
  6. <artifactId>MavenExample</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. <name>MavenExample</name>
  9. <!-- FIXME change it to the project's website -->
  10. <url>http://maven.apache.org</url>
  11. <dependencies>
  12. <dependency>
  13. <groupId>junit</groupId>
  14. <artifactId>junit</artifactId>
  15. <version>4.11</version>
  16. <scope>test</scope>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.apache.commons</groupId>
  20. <artifactId>commons-lang3</artifactId>
  21. <version>3.10</version>
  22. <scope>compile</scope>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.apache.commons</groupId>
  26. <artifactId>commons-collections4</artifactId>
  27. <version>4.0</version>
  28. <scope>compile</scope>
  29. </dependency>
  30. </dependencies>
  31. <build>
  32. <sourceDirectory>src</sourceDirectory>
  33. <plugins>
  34. <plugin>
  35. <artifactId>maven-compiler-plugin</artifactId>
  36. <version>3.7.0</version>
  37. <configuration>
  38. <source>1.8</source>
  39. <target>1.8</target>
  40. </configuration>
  41. </plugin>
  42. </plugins>
  43. </build>
  44. </project>

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

  1. package it.maven.project;
  2. import org.apache.commons.lang3.RandomStringUtils;
  3. /**
  4. * Hello world!
  5. *
  6. */
  7. public class App
  8. {
  9. public static void main( String[] args )
  10. {
  11. System.out.println( "Hello World!" );
  12. System.out.println("Stringa generata casualmente: " + RandomStringUtils.random(16, true, true).toUpperCase());
  13. }
  14. }

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

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

  1. 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:

  1. 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:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  3. xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
  4. &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  5. &lt;groupId&gt;it.maven.project&lt;/groupId&gt;
  6. &lt;artifactId&gt;MavenExample&lt;/artifactId&gt;
  7. &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  8. &lt;name&gt;MavenExample&lt;/name&gt;
  9. &lt;!-- FIXME change it to the project&#39;s website --&gt;
  10. &lt;url&gt;http://maven.apache.org&lt;/url&gt;
  11. &lt;dependencies&gt;
  12. &lt;dependency&gt;
  13. &lt;groupId&gt;junit&lt;/groupId&gt;
  14. &lt;artifactId&gt;junit&lt;/artifactId&gt;
  15. &lt;version&gt;4.11&lt;/version&gt;
  16. &lt;scope&gt;test&lt;/scope&gt;
  17. &lt;/dependency&gt;
  18. &lt;dependency&gt;
  19. &lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
  20. &lt;artifactId&gt;commons-lang3&lt;/artifactId&gt;
  21. &lt;version&gt;3.10&lt;/version&gt;
  22. &lt;scope&gt;compile&lt;/scope&gt;
  23. &lt;/dependency&gt;
  24. &lt;dependency&gt;
  25. &lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
  26. &lt;artifactId&gt;commons-collections4&lt;/artifactId&gt;
  27. &lt;version&gt;4.0&lt;/version&gt;
  28. &lt;scope&gt;compile&lt;/scope&gt;
  29. &lt;/dependency&gt;
  30. &lt;/dependencies&gt;
  31. &lt;build&gt;
  32. &lt;sourceDirectory&gt;src&lt;/sourceDirectory&gt;
  33. &lt;plugins&gt;
  34. &lt;plugin&gt;
  35. &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
  36. &lt;version&gt;3.7.0&lt;/version&gt;
  37. &lt;configuration&gt;
  38. &lt;source&gt;1.8&lt;/source&gt;
  39. &lt;target&gt;1.8&lt;/target&gt;
  40. &lt;/configuration&gt;
  41. &lt;/plugin&gt;
  42. &lt;/plugins&gt;
  43. &lt;/build&gt;
  44. &lt;/project&gt;

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

  1. package it.maven.project;
  2. import org.apache.commons.lang3.RandomStringUtils;
  3. /**
  4. * Hello world!
  5. *
  6. */
  7. public class App
  8. {
  9. public static void main( String[] args )
  10. {
  11. System.out.println( &quot;Hello World!&quot; );
  12. System.out.println(&quot;Stringa generata casualmente: &quot; + RandomStringUtils.random(16, true, true).toUpperCase());
  13. }
  14. }

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:

确定