当我尝试运行打包为胖子的Spring Boot应用程序时,我遇到了奇怪的问题。
该应用程序可与Eclipse和mvn spring-boot:run
完美配合,但是当我使用java -jar myjar
时,休眠实体未映射。
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Entity is not mapped
at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:169) ~[hibernate-core-5.2.17.Final.jar!/:5.2.17.Final]
我知道mvn spring-boot:run
是不同的,因为它会更改类路径,但是有没有一种方法可以以冗长的方式运行它,因此我可以看到类路径是什么,以便可以将java -jar
用于类似的东西?
当我打印System.getProperty("java.class.path");
我得到以下
Classpath:/usr/local/apache-maven/boot/plexus-classworlds-2.5.2.jar
当我从java -jar myJar.jar
运行它时,我得到myJar.jar
,所以这可能就是它不起作用的原因。
以下是pom文件:
Api(包含主类)
<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.foo</groupId>
<artifactId>api</artifactId>
<version>0.0.2-SNAPSHOT</version>
<packaging>jar</packaging>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>com.foo</groupId>
<artifactId>service</artifactId>
<version>0.0.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-jersey2-jaxrs</artifactId>
<version>1.5.13</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>jar-with-all-dependencies</classifier>
<mainClass>
${main-class-path}
</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
服务(包含休眠逻辑)
<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.foo</groupId>
<artifactId>service</artifactId>
<version>0.0.2-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org-springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org-springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>${org-springframework-data.version}</version>
</dependency>
<dependency>
<groupId>org.threeten</groupId>
<artifactId>threetenbp</artifactId>
<version>1.3.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org-springframework.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey-media-json-jackson.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate-core.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>${querydsl.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>${apt-maven-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/apt</outputDirectory>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
这是主要班级
@SpringBootApplication
@ComponentScan("com.foo")
public class Application extends SpringBootServletInitializer {
private static final Logger LOG = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
if (!argurmentsValid(args)) {
return;
}
final SpringApplication app = new SpringApplication(Application.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
}
}
这是api中的配置
@Configuration
@ApplicationPath("/api")
public class ApiConfiguration extends ResourceConfig {
public static final String CONFIG_FILE_NAME = "api.properties";
public ApiConfiguration() {
register(com.foo.api.v1.controllers.aController.class);
}
}
这是服务库中的配置
@Configuration
@ComponentScan("com.foo.service")
@EnableTransactionManagement
public class ServiceConfiguration {
@Bean
public PersistenceAnnotationBeanPostProcessor persistenceAnnotationBeanPostProcessor() {
return new PersistenceAnnotationBeanPostProcessor();
}
@Bean
public LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean () {
return new LocalContainerEntityManagerFactoryBean();
}
@Bean
public JpaTransactionManager jpaTransactionManager() {
return new JpaTransactionManager();
}
}
并且我在com.foo.service中有常规的休眠实体。
我用mvn clean install
包装罐子,然后用java -jar jar-with-all-dependencies.jar
运行罐子
答案 0 :(得分:0)
我需要知道一些详细信息才能为您提供帮助:
1-您是如何包装到肥罐的? 2-您如何运行它?
无法像您提到的那样,将Springboot像其他应用程序一样打包在胖子罐中,它会更改类路径以能够正确使用springboot自动配置。
我最近为springboot应用程序创建了一个fatjar,它运行良好,但是我不得不使用重新包装的gradle插件正确地重新组装jar。该插件是由spring制作的,您可能与maven相同。
您看过这里吗?:https://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-maven-plugin.html
ALSO
请记住,即使您未正确配置应用程序,Eclipse也会执行一些类路径魔术操作来完成工作。
对于fatJar,您需要:
关于最后一点,我注意到在使用java -jar运行时springboot打包未考虑我的-D参数,我不得不使用JavaExec gradle插件。