簡體   English   中英

為什么 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);
    }
}

完成錯誤信息

[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

我閱讀了很多關於 stackoverflow 的文章和很多相關問題。 但 maven 仍然無法下載 repo。 並且構建失敗。 我使用 mvn compile、mvn package、mvn clean install 對我沒有任何作用。 請幫忙✌️

根據 文檔,您需要擁有所有三個依賴項。 您發布的那個問題與 maven 沒有直接關系。 這表示編譯器找不到符號,可能是因為 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM