![](/img/trans.png)
[英]Eclipse Maven - Code Completion fails "This compilation unit is not on the build path of a Java project" and "Failed to Download Index" Error
[英]Java project in IntelliJ is error free but compilation fails via Maven
我已经通过示例hello world dropwizard项目进行了工作,该项目在IntelliJ中似乎没有错误(我是Eclipse的资深人士,但是对IntelliJ还是新手),但是通过maven编译失败。
源目录包含:
/src/main/java/com/example/helloworld/HelloWorldConfiguration.java
/src/main/java/com/example/helloworld/HelloWorldService.java
HelloWorldService
如下所示:
package com.example.helloworld;
import com.example.helloworld.resources.HelloWorldResource;
import com.example.helloworld.health.TemplateHealthCheck;
import com.yammer.dropwizard.Service;
import com.yammer.dropwizard.config.Bootstrap;
import com.yammer.dropwizard.config.Environment;
public class HelloWorldService extends Service<HelloWorldConfiguration> {
public static void main(String[] args) throws Exception {
new HelloWorldService().run(args);
}
@Override
public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap) {
bootstrap.setName("hello-world");
}
@Override
public void run(HelloWorldConfiguration configuration,
Environment environment) {
final String template = configuration.getTemplate();
final String defaultName = configuration.getDefaultName();
environment.addResource(new HelloWorldResource(template, defaultName));
environment.addHealthCheck(new TemplateHealthCheck(template));
}
}
Maven回来了:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project my-project: Compilation failure: Compilation failure:
[ERROR] /Users/markdsievers/IdeaProjects/dropwizard/src/main/java/com/example/helloworld/HelloWorldService.java:[9,48] cannot find symbol
[ERROR] symbol: class HelloWorldConfiguration
[ERROR] /Users/markdsievers/IdeaProjects/dropwizard/src/main/java/com/example/helloworld/HelloWorldService.java:[15,38] cannot find symbol
[ERROR] symbol : class HelloWorldConfiguration
[ERROR] location: class com.example.helloworld.HelloWorldService
[ERROR] /Users/markdsievers/IdeaProjects/dropwizard/src/main/java/com/example/helloworld/HelloWorldService.java:[20,21] cannot find symbol
[ERROR] symbol : class HelloWorldConfiguration
[ERROR] location: class com.example.helloworld.HelloWorldService
pom.xml
如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<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.example.helloworld</groupId>
<artifactId>my-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.yammer.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>0.6.2</version>
</dependency>
</dependencies>
<properties>
<!-- use UTF-8 for everything -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
如果编译目标是从命令行或在IntelliJ中运行的,我也会从maven中得到相同的错误。 源根目录(/ src / main / java)是在IntelliJ中设置的,没有任何清理和重新编译的影响。
任何建议表示赞赏。
找到了问题... HelloWorldConfiguration
没有.java
扩展名。 非常邪恶的IMO,IDE将其视为有效的Java文件,并且是通过New Java类创建的(或必须没有,否则会出问题)。 无论如何,可能需要启用文件扩展名的视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.