首先让我说我正在学习Java,来自.NET/C#世界。
太长别读:这种语法在VSCode或Eclipse中不起作用IDE在IntelliJ中起作用。为什么?我错过了什么?
import path.to.class.Foo;
import path.to.class.Foo_; // cannot be resolved error
...
Foo_.barID; // cannot resolve to a variable
underscore_符号只是导致上面的错误
长版本:
我在API工作,我的团队是这个项目的新手。我们都不是JAVA特定的开发人员,在过去的几个月里,我们学到了很多东西。最初的开发人员使用IntelliJ创建了这个项目,并使用IDE支持它。不幸的是,我们的IntelliJ许可证一直被允许过期,需要一周时间才能恢复,我不擅长停机,所以我尝试了Eclipse和VSCode。当尝试调试时,项目无法构建,因为一些导入无法解析,以及一些似乎使用JPA 2.0表示法进行动态、类型安全查询的变量。阅读下面的内容,这个符号似乎已经存在了很长时间,而EclipseJavaIDE很长时间,所以我认为我显然遗漏了一些东西。连接到类名的下划线是什么意思?https://developer.ibm.com/articles/j-typesafejpa/#N102F2https://developer.ibm.com/articles/j-typesafejpa/
我很难相信这只在IntelliJ中是可能的,所以它必须是我在IDE中缺少的东西。一个包,或者设置,或者不允许IDE使用标准API的东西?
请求的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">
<parent>
<artifactId>foo-app</artifactId>
<groupId>foo.bar.app</groupId>
<version>0.4.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>bar-api</artifactId>
<name>Public Facing API</name>
<properties>
<mainClass>foo.bar.fib.api.ApiService</mainClass>
<jjwt.version>0.11.4</jjwt.version>
</properties>
<dependencies>
<dependency>
<groupId>foo.bar.fib</groupId>
<artifactId>fib-testing</artifactId>
<version>0.4.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-client</artifactId>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-migrations</artifactId>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-hibernate</artifactId>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-auth</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
<dependency>
<groupId>com.hubspot.dropwizard</groupId>
<artifactId>dropwizard-guicier</artifactId>
</dependency>
<dependency>
<groupId>${typesafe.config.groupID}</groupId>
<artifactId>typesafe-dropwizard-configuration</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>foo.bar.fib</groupId>
<artifactId>fib-queue</artifactId>
<version>0.4.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>foo.bar.fib</groupId>
<artifactId>fib-common</artifactId>
<version>0.4.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>foo.bar.fib</groupId>
<artifactId>fib-tokens</artifactId>
<version>0.4.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>foo.bar.fib</groupId>
<artifactId>fib-buttonstuff</artifactId>
<version>0.4.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>${hapi.fhir.groupID}</groupId>
<artifactId>hapi-fhir-client</artifactId>
</dependency>
<dependency>
<groupId>com.jakewharton.fliptables</groupId>
<artifactId>fliptables</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>${bouncey.version}</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>${bouncey.version}</version>
</dependency>
<dependency>
<groupId>${hapi.fhir.groupID}</groupId>
<artifactId>hapi-fhir-structures-r4</artifactId>
</dependency>
<dependency>
<groupId>${hapi.fhir.groupID}</groupId>
<artifactId>hapi-fhir-validation-resources-dstu3</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.helger</groupId>
<artifactId>ph-schematron</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>${jjwt.version}</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>${jjwt.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>${jjwt.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.9.3</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>com.newrelic.agent.java</groupId>
<artifactId>newrelic-java</artifactId>
<version>${newrelic.agent.version}</version>
<type>${newrelic.agent.type}</type>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-json-logging</artifactId>
</dependency>
<!--Test resources-->
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-testing</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<version>2.31</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>${project.basedir}/../src/main/resources</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<testResources>
<testResource>
<directory>${project.basedir}/../src/main/resources</directory>
</testResource>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>
<plugins>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>${mainClass}</mainClass>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<shadedArtifactAttached>true</shadedArtifactAttached>
<finalName>${project.artifactId}</finalName>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>${mainClass}</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<configuration>
<container>
<args>
<arg>server</arg>
</args>
<ports>
<port>8080</port>
</ports>
<environment>
<DB_MIGRATION>1</DB_MIGRATION>
</environment>
<entrypoint>/entrypoint.sh</entrypoint>
</container>
<extraDirectories>
<paths>
<path>${project.basedir}/../bbcerts</path>
<path>${project.basedir}/target/jacoco-agent</path>
<path>${project.basedir}/docker</path>
<path>${project.basedir}/../src/main/resources/keypair</path>
<path>${project.basedir}/target/newrelic-agent</path>
</paths>
<permissions>
<permission>
<file>/entrypoint.sh</file>
<mode>755</mode>
</permission>
</permissions>
</extraDirectories>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<annotationProcessorPath>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>5.4.2.Final</version>
</annotationProcessorPath>
<path>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</path>
<path>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.1</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
编辑:不再监控这个问题,因为使用IntelliJ IDEA的社区版将满足我的需求,我不必切换IDE。我认为建议的答案将适用于一些人,如果不是大多数人的话;然而,由于我无法(也不愿意继续)使构建与另一个IDE一起工作,我没有选择它作为解决方案。
这些是实体的所谓元模型类,它们由Hibernate生成(因为您在pom中配置了hibernate-jpamodelgen
注释处理器)。因此,每当运行maven构建时,这些类都会生成到特定目录中,然后Maven在编译步骤中将该目录作为源位置包含在内。
由于Eclipse(我想还有VSCode)不知道这个生成文件的文件夹是项目的源位置,因此它无法识别生成的类。有几种方法可以处理它:
>
您可以在Eclipse中配置注释处理器,使其执行与Maven插件相同的操作。它非常简单,但需要一次性手动设置。这里找到了一组不错的指令;官方的JBoss Hibernate指令非常过时,但可能仍然有效,在这里找到(该页面上还有对Hibernate元模型生成的不错的解释,以供参考)。
请注意,在选项2中,当您更改实体时,您必须重新运行maven构建(至少生成源阶段)以查看更新的生成类。
使用@joopeggen提供的IntelliJ社区版。@e-riz提出的答案可能是次要答案。