为什么 maven 不能将我的依赖项添加到 jar?

Why maven cannot add my dependency to jar?

我是 Maven 的新手,我想做的一件简单的事情就是拉取 Maven 存储库并在我的项目中使用它。 但这已经发生了。

pom.xml

<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/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>ToolsQA</groupId>

  <artifactId>jutil_table</artifactId>

  <packaging>jar</packaging>

  <version>1.0-SNAPSHOT</version>

  <name>jutil_table</name>

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

  <dependencies>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>com.github.chrisgleissner</groupId>
      <artifactId>jutil-protobuf</artifactId>
      <version>1.1.11</version>
    </dependency>

  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

App.java

package ToolsQA;

/**
 * Hello world!
 *
 */

 import static com.github.chrisgleissner.jutil.table.TablePrinter.DefaultTablePrinter;
 import static java.util.Arrays.asList;


public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
         Iterable<String> HEADERS = asList("id", "name", "age");
         DefaultTablePrinter.print(HEADERS, null);
    }
}

Complete Error message

[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------< ToolsQA:jutil_table >-------------------------
[INFO] Building jutil_table 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ jutil_table ---
[INFO] Deleting C:\Users\Pawar\Desktop\mvn_tutorial\jutil_table\jutil_table\target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ jutil_table ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\Pawar\Desktop\mvn_tutorial\jutil_table\jutil_table\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.5.1:compile (default-compile) @ jutil_table ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:\Users\Pawar\Desktop\mvn_tutorial\jutil_table\jutil_table\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /C:/Users/Pawar/Desktop/mvn_tutorial/jutil_table/jutil_table/src/main/java/ToolsQA/App.java:[8,53] package com.github.chrisgleissner.jutil.table does not exist
[ERROR] /C:/Users/Pawar/Desktop/mvn_tutorial/jutil_table/jutil_table/src/main/java/ToolsQA/App.java:[8,2] static import only from classes and interfaces
[ERROR] /C:/Users/Pawar/Desktop/mvn_tutorial/jutil_table/jutil_table/src/main/java/ToolsQA/App.java:[18,10] cannot find symbol
  symbol:   variable DefaultTablePrinter
  location: class ToolsQA.App
[INFO] 3 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.037 s
[INFO] Finished at: 2020-05-24T22:49:15+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.5.1:compile (default-compile) on project jutil_table: Compilation failure: Compilation failure: 
[ERROR] /C:/Users/Pawar/Desktop/mvn_tutorial/jutil_table/jutil_table/src/main/java/ToolsQA/App.java:[8,53] package com.github.chrisgleissner.jutil.table does not exist
[ERROR] /C:/Users/Pawar/Desktop/mvn_tutorial/jutil_table/jutil_table/src/main/java/ToolsQA/App.java:[8,2] static import only from classes and interfaces
[ERROR] /C:/Users/Pawar/Desktop/mvn_tutorial/jutil_table/jutil_table/src/main/java/ToolsQA/App.java:[18,10] cannot find symbol
[ERROR]   symbol:   variable DefaultTablePrinter
[ERROR]   location: class ToolsQA.App
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

我在 Whosebug 上阅读了很多文章和很多相关问题。但 Maven 仍然无法下载回购协议。并且构建失败。 我使用 mvn compile、mvn package、mvn clean install 对我没有任何作用。 请帮忙✌️

根据 documentation,您需要拥有所有三个依赖项。您发布的那个问题与 Maven 没有直接关系。这表示编译器找不到符号,可能是因为 class 不在 class 路径中。尝试包含所有依赖项。

或者在这种情况下,您真正​​需要的只是 jutil-table 的依赖项。你可以省略其他两个。但是您的 Maven pom 包含 jutil-protobuf 依赖项。

<dependency>
    <groupId>com.github.chrisgleissner</groupId>
    <artifactId>jutil-protobuf</artifactId>
    <version>1.1.11</version>
</dependency>
<dependency>
    <groupId>com.github.chrisgleissner</groupId>
    <artifactId>jutil-sql-log</artifactId>
    <version>1.1.11</version>
</dependency>
<dependency>
    <groupId>com.github.chrisgleissner</groupId>
    <artifactId>jutil-table</artifactId>
    <version>1.1.11</version>
</dependency>

并使用 maven 程序集插件构建包含依赖项的 jar

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass> // Main class Ex : com.Test </mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>