简体   繁体   English

Maven如何正确包含依赖项

[英]maven how to include dependencies correctly

I was trying to figuring out how to use maven but I didn't get it. 我试图弄清楚如何使用Maven,但我没有明白。

First I tried sqlite 3.8.7 from : 首先,我尝试了来自的sqlite 3.8.7:

mvnrepository.com/artifact/org.xerial/sqlite-jdbc/3.8.7 mvnrepository.com/artifact/org.xerial/sqlite-jdbc/3.8.7

I can compile well but when I try to execute maven doesn't find sqlite jar file, so I try to use : 我可以很好地编译,但是当我尝试执行maven时,找不到sqlite jar文件,因此我尝试使用:

mvn install:install-file mvn install:安装文件

but It didn't work too, so I just used -cp and I have fixed. 但是它也没有用,所以我只用了-cp并且已经修复。

Second I try to use jfreechart from : 其次,我尝试从使用jfreechart:

http://mvnrepository.com/artifact/jfree/jfreechart/1.0.13 http://mvnrepository.com/artifact/jfree/jfreechart/1.0.13

I did same steps below for jfreechart but this time it gives me NoClassDefFoundError. 我在下面对jfreechart执行了相同的步骤,但是这次它给了me NoClassDefFoundError.

Both of them works when I compile manually but with maven it's not. 当我手动编译时,它们都可以工作,但使用maven则不能。 What I am missing about it ? 我对此缺少什么? If I always add manually like sqlite why should use maven anyway ? 如果我总是像sqlite一样手动添加,为什么仍要使用maven?

Notes : I compile as : 注意:我编译为:

mvn compile mvn编译

and I'm packaging as : 和我包装为:

mvn package mvn包

and finally I try to execute as : 最后我尝试执行为:

java -cp target/porject.jar org.path.App java -cp target / porject.jar org.path.App

Edit : 编辑:

This is for jfreechart app(pom.xml) I add dependency tags from mvnrepository.com 这是用于jfreechart应用程序(pom.xml)我从mvnrepository.com添加依赖项标签

<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>org.project</groupId>
  <artifactId>ChartTest</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>ChartTest</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>jfree</groupId>
        <artifactId>jfreechart</artifactId>
        <version>1.0.13</version>
    </dependency>
  </dependencies>
</project>

Thanks in Advance 提前致谢

All that you need to create your jar executable with your dependencies and launch it as a standalone app: 创建带有依赖项的jar可执行文件并将其作为独立应用程序启动所需的所有操作:

<build>
  <plugins>
    <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>com.mypackage.main.Main</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
    </plugin>
  </plugins>
</build>

and then run goal : 然后运行目标:

clean package assembly:single 

Your problem is you don't include your dependencies in your target jar. 您的问题是您没有在目标jar中包含依赖项。

Add this to your pom.xml: 将此添加到您的pom.xml中:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.1</version>
            <configuration>

                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>

            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id> <!-- this is used for inheritance merges -->
                    <phase>package</phase> <!-- append to the packaging phase. -->
                    <goals>
                        <goal>attached</goal> <!-- goals == mojos -->
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Then you can start your app using the command line: 然后,您可以使用命令行启动您的应用程序:

java -cp target/ChartTest-1.0-SNAPSHOT-jar-with-dependencies.jar org.path.App

不要在命令中使用连字符,使用mvn packagemvn compile

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

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