Adding repackage goal leads to "Error creating bean with name 'entityManagerFactory'"

bnlucas :

I'm working on dockerizing a Spring Boot app and I'm running into some issues:

  • Running mvn spring-boot:run works just fine.
  • Before adding the repackage goal, I get no main manifest attribute, in target/app.war when running java -jar target/app.war
  • After adding the repackage goal, I get an error with creating entityManagerFactory when running java -jar target/app.war
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.boot.archive.spi.ArchiveException: Could not build ClassFile
...
***************************
APPLICATION FAILED TO START
***************************

Description:

Field profileRepository in com.bnlucas.app.util.PostTransactionUtil required a bean named 'entityManagerFactory' that could not be found.


Action:

Consider defining a bean named 'entityManagerFactory' in your configuration.

I don't know much about the inner workings of Maven, and can't figure out what could be happening. I had an issue with javassist dependency issues, and tried excluding it and got a little further with what all was happening.

<plugin>
  <groupId>org.liquibase</groupId>
  <artifactId>liquibase-maven-plugin</artifactId>
  <version>${liquibase.version}</version>
  <dependencies>
    <dependency>
      <groupId>org.javassist</groupId>
      <artifactId>javassist</artifactId>
      <version>3.21.0-GA</version>
    </dependency>
    <dependency>
      <groupId>org.liquibase.ext</groupId>
      <artifactId>liquibase-hibernate4</artifactId>
      <version>${liquibase-hibernate4.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
      <version>${spring.boot.version}</version>
      <exclusions>
        <exclusion>
          <groupId>org.javassist</groupId>
          <artifactId>javassist</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>
  ...
</plugin>

Trying things I've found across the site, I've changed the the entity manager ref:

@EnableJpaRepositories(
  basePackages = {"com.bnlucas.app.repository"},
  entityManagerFactoryRef = "entityManagerF"
)

and just got

***************************
APPLICATION FAILED TO START
***************************

Description:

Field profileRepository in com.bnlucas.app.util.PostTransactionUtil required a bean named 'entityManagerF' that could not be found.


Action:

Consider defining a bean named 'entityManagerF' in your configuration.

I've also tried defining the bean with LocalContainerEntityManagerFactoryBean and still nothing.

@Bean
public EntityManagerFactory entityManagerFactory(DataSourceProperties dataSourceProperties) {
    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    vendorAdapter.setGenerateDdl(Boolean.TRUE);
    vendorAdapter.setShowSql(Boolean.TRUE);
    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
    factory.setJpaVendorAdapter(vendorAdapter);
    factory.setPackagesToScan("com.bnlucas.app.repository");
    factory.setDataSource(dataSource(dataSourceProperties));
    factory.afterPropertiesSet();
    factory.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
    return factory.getObject();
}

What is mvn spring-boot:run doing that's different than running mvn package spring-boot:repackage? How is the entity manager defined with the first and fails with the latter?

DatabaseConfiguration: https://gist.github.com/bnlucas/3e9db7d67aa52e43e8013a0cb81b40b3#file-databaseconfiguration-java

pom: https://gist.github.com/bnlucas/3e9db7d67aa52e43e8013a0cb81b40b3#file-pom-xml

bnlucas :

After troubleshooting and narrowing things down, I found the issue. It was a filtering flag in the maven war plugin.

Removing the <filtering /> here got everything up and running

<resource>
  <directory>.</directory>
  <targetPath>META-INF</targetPath>
  <filtering>true</filtering>
</resource>
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-war-plugin</artifactId>
  <version>2.4</version>
  <configuration>
    <webResources>
      <resource>
        <!-- this is relative to the pom.xml directory -->
        <directory>config</directory>
        <includes>
          <include>build-${project.artifactId}.properties</include>
        </includes>
        <!-- override the destination directory for this resource -->
        <targetPath>META-INF</targetPath>
        <!-- enable filtering -->
        <filtering>true</filtering>
      </resource>
      <resource>
        <!-- this is relative to the pom.xml directory -->
        <directory>config</directory>
        <includes>
          <include>build-${project.artifactId}.properties</include>
        </includes>
        <!-- override the destination directory for this resource -->
        <targetPath>WEB-INF/classes</targetPath>
        <!-- enable filtering -->
        <filtering>true</filtering>
      </resource>
      <resource>
        <directory>.</directory>
        <targetPath>META-INF</targetPath>
      </resource>
    </webResources>
  </configuration>
</plugin>

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=418128&siteId=1