IntelliJ中的Java项目没有错误,但通过Maven编译失败
我已经完成了这个项目,它在IntelliJ中看起来没有错误(我是Eclipse的老手,但对IntelliJ来说是新手),但是通过maven编译失败了 源目录包含:IntelliJ中的Java项目没有错误,但通过Maven编译失败,java,maven,intellij-idea,Java,Maven,Intellij Idea,我已经完成了这个项目,它在IntelliJ中看起来没有错误(我是Eclipse的老手,但对IntelliJ来说是新手),但是通过maven编译失败了 源目录包含: /src/main/java/com/example/helloworld/HelloWorldConfiguration.java /src/main/java/com/example/helloworld/HelloWorldService.java 其中HelloWorldService看起来像: package com.e
/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));
}
}
<?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>
其中,pom.xml
如下所示:
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));
}
}
<?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>
4.0.0
com.example.helloworld
我的项目
0.0.1-快照
com.yammer.dropwizard
dropwizard核心
0.6.2
UTF-8
UTF-8
org.apache.maven.plugins
maven编译器插件
3.1
1.6
1.6
UTF-8
如果编译目标是从命令行或IntelliJ中运行的,那么maven也会出现同样的错误。源根(/src/main/java)是在IntelliJ中设置的,没有任何清理和重新编译的效果
感谢您的建议。发现了问题
HelloWorldConfiguration
没有.java
扩展名。在我看来,IDE将其视为一个有效的java文件,并且是通过新的java类创建的(或者必须没有,或者出了问题)。无论如何,可能需要启用文件扩展名视图。如果您也包含HelloWorldConfiguration
的完整源代码,则更容易再现这种情况。(我遇到了与此相关的其他编译问题。你的设置没有发现任何明显的问题。)正如答案所证明的,“太本地化了”。这很奇怪,但很高兴你让它工作起来。(我真的很难相信,尽管通常通过IDEA UI创建类时,IDEA会忽略“.java”扩展。)@Jonik Yes,完全搞不清它是如何进入(并保持)那种状态的,而且IntelliJ表示环境是无错误的,这让人恼火。感谢您的关注,很抱歉出现了非常局部的问题。