maven错误:无法在包中找到符号类

时间:2018-04-24 15:21:51

标签: java maven

这是我第一次使用maven,我试图了解它是如何工作的。因此,我有一个小例子,在不同的包中有两个类。

package maven.callee ;

public class Callee {
  public void callMe() {
   System.out.println("Callee says: You called me!");
  }
}

现在是具有主要功能的Caller类。

package maven.caller ;
import maven.callee.Callee ;

public class Caller {
  public static void main(String [] args) {
   Callee callee = new Callee ();
   callee.callMe ();
  }
}

我知道src目录的结构与标准不一样。 当我在pom.xml文件上运行命令mvn package时,我得到以下输出:

[INFO] Compiling 1 source file to /home/ced/workspaceForOxygene/build/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /home/ced/workspaceForOxygene/build/src/maven/caller/Caller.java:[2,20] package maven.callee does not exist
[ERROR] /home/ced/workspaceForOxygene/build/src/maven/caller/Caller.java:[6,4] cannot find symbol
  symbol:   class Callee
  location: class maven.caller.Caller
[ERROR] /home/ced/workspaceForOxygene/build/src/maven/caller/Caller.java:[6,24] cannot find symbol
  symbol:   class Callee
  location: class maven.caller.Caller
[INFO] 3 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.645 s
[INFO] Finished at: 2018-04-24T17:12:36+02:00
[INFO] Final Memory: 13M/158M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.2:compile (default-compile) on project mavenTest: Compilation failure: Compilation failure:
[ERROR] /home/ced/workspaceForOxygene/build/src/maven/caller/Caller.java:[2,20] package maven.callee does not exist
[ERROR] /home/ced/workspaceForOxygene/build/src/maven/caller/Caller.java:[6,4] cannot find symbol
[ERROR] symbol:   class Callee
[ERROR] location: class maven.caller.Caller
[ERROR] /home/ced/workspaceForOxygene/build/src/maven/caller/Caller.java:[6,24] cannot find symbol
[ERROR] symbol:   class Callee
[ERROR] location: class maven.caller.Caller
[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

请发生什么事情?为什么不能找到那个文件? hier是我的POM文件,我做错了什么?

<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>com.swt.build</groupId>
    <artifactId>mavenTest</artifactId>
    <version>1.0</version>

    <build>
        <sourceDirectory>src/maven/caller</sourceDirectory>
        <!--specify output directory for binaries -->
        <outputDirectory> classes </outputDirectory>

           <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins </groupId>
                <artifactId> maven-jar-plugin</artifactId>
                <version> 3.0.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath> true </addClasspath>
                            <mainClass> maven.caller.Caller </mainClass>                        
                        </manifest>
                    </archive> 
                    <!--specify output directory for jar file -->
                    <outputDirectory>jars</outputDirectory>   
                </configuration>
            </plugin>
        </plugins>

      </build>      
</project>  

1 个答案:

答案 0 :(得分:1)

maven.callee is missing from compiler path. It should be part of source dir or should be defined as lib dependency. With the current setting, Maven will try to compile the java files only under src/maven/caller. Change sourceDirectory to src/maven in pom.xml.