简体   繁体   中英

Why maven cannot add my dependency to jar?

I'm new to maven and one simple thing I wanted to do is pull maven repository and use it in my project. but this has happened.

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

I read lot of articles and lot of related questions on stackoverflow about this. but still maven is unable to download the repo. and build is failing. I used mvn compile, mvn package, mvn clean install nothing worked for me. Please help✌️

As per the documentation , you need to have all three dependencies. That issue you have posted is not directly relevant to maven. That says the compiler cannot find symbols, may be because the class is not there in the classpath. Try with all the dependencies included.

Or in this case, what you really need the dependency for jutil-table only. You can omit the other two. But your maven pom contain jutil-protobuf dependency.

<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>

And build the jar including the dependencies using maven assembly plugin

        <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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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