Package accessible from more than one module in Intellij - java

I'm trying to use google text to speech but there is a problem with the imports:
import com.google.cloud.texttospeech.v1beta1.AudioConfig;
import com.google.cloud.texttospeech.v1beta1.AudioEncoding;
import com.google.cloud.texttospeech.v1beta1.SsmlVoiceGender;
import com.google.cloud.texttospeech.v1beta1.SynthesisInput;
import com.google.cloud.texttospeech.v1beta1.SynthesizeSpeechResponse;
import com.google.cloud.texttospeech.v1beta1.TextToSpeechClient;
import com.google.cloud.texttospeech.v1beta1.VoiceSelectionParams;
It shows various errors where it's using the same modules from different packages:
java: module google.cloud.texttospeech reads package com.google.cloud.texttospeech.v1beta1 from both google.cloud.texttospeech and proto.google.cloud.texttospeech.v1beta1
I have these requirements in my module-info file:
requires proto.google.cloud.texttospeech.v1beta1;
requires google.cloud.texttospeech;
I also get a yellow warning with these requirements:
Name of automatic module 'proto.google.cloud.texttospeech.v1beta1' is unstable, it is derived from the module's file name.
pom.xml is as follows (removed project tag for clarity):
<modelVersion>4.0.0</modelVersion>
<groupId>com.cloze</groupId>
<artifactId>cloze</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>26.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>18.0.1</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-media</artifactId>
<version>18.0.1</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-texttospeech</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.6</version>
<executions>
<execution>
<!-- Default configuration for running -->
<!-- Usage: mvn clean javafx:run -->
<id>default-cli</id>
<configuration>
<mainClass>com.cloze.App</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Contact the maintainers of the google-cloud-texttospeech library and ask them to provide documentation (and perhaps an updated implementation) for using their library from a modular Java app.
In the meantime go to openjfx.io and review documentation on non-modular JavaFX apps and try following it.
Asker writes:
If I remove the module-info file to make it non modular that does indeed solve this problem.

Related

Dependency not accept

I have this problem : My dependency org.apache.poi can not accept in pom.xml
this dependency lighting red. But i add in project module jars:
poi-5.2.2
poi-ooxml-5.2.2
commons-collections 4-4.3
commons-compress-1.18
xmlbeans-3.1.0
poi-ooxml-schemas-3.9
dom4j-1.6.1
all jars i am add but dependency not accepted and continue lighting red: my code
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.2</version>
</dependency>
Those Maven coordinates are correct.
Here is an example POM based on the artifact maven-archetype-quickstart, with updated versions on all items, and with your your two dependencies pasted as-is. And I updated the import statements in the AppTest.java file to use JUnit Jupiter. So we have fully-working, up-to-date, practical example app.
Your dependencies are processed correctly by Maven. This app compiles and runs. I did this in IntelliJ 2022.1.
<?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>work.basil.example</groupId>
<artifactId>TryDep</artifactId>
<version>1.0-SNAPSHOT</version>
<name>TryDep</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.2</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.9.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>3.0.0-M1</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.0.0-M1</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.9.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.2.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
As a sanity check, I suggest you create and run a new project using that POM posted above.
In your own problematic project, I suggest you make sure Maven processed your POM. In IntelliJ, either:
Click the little floating windoid with a Maven logo.
Click the two arrows in a circle icon in the Maven panel, a button whose tooltip says Reload All Maven Projects.
Then execute a Maven clean and install.
Your locale Maven cache in a .m2 folder may need to download the dependencies which may take several minutes depending on the speed of your Internet access.
On occasion, the Maven local cache goes wonky. If all else fails, delete the entire .m2 folder. Then do another clean and install which in turn should trigger creation and population of a fresh .m2 folder.

Facing some problem while defining maven.compiler in pom.xml

A)Summarize the problem
There are three methods to define compiler version in pom maven.
I did not mention maven.compiler release , plugin and version it works (means maven build success)then how it is picking the compiler version and i did not mention version
For The things ihave tried please refer section b
1)
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<verbose>true</verbose>
</configuration>
</plugin>
<maven.compiler.release> 8</maven.compiler.release>
B) What i have tried
I remove both maven.compiler release and plugin it works then how it is picking the compiler version
<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>io.flowing.retail</groupId>
<artifactId>flowing-retail-kafka-shipping</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.boot.version>2.2.5.RELEASE</spring.boot.version>
<spring-cloud-stream.version>Horsham.RELEASE</spring-cloud-stream.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-dependencies</artifactId>
<version>${spring-cloud-stream.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-kafka</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<!-- required to add JSR310 time formats for Jackson to ObjectMapper -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<mainClass>io.flowing.retail.shipping.ShippingApplication</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I define invalid maven.compiler.release. It then also works
4.0.0
io.flowing.retail
flowing-retail-kafka-shipping
0.0.1-SNAPSHOT
<maven.compiler.release>108</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.boot.version>2.2.5.RELEASE</spring.boot.version>
<spring-cloud-stream.version>Horsham.RELEASE</spring-cloud-stream.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-dependencies</artifactId>
<version>${spring-cloud-stream.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-kafka</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<!-- required to add JSR310 time formats for Jackson to ObjectMapper -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<mainClass>io.flowing.retail.shipping.ShippingApplication</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
[Let me first clarify the expression "picking the compiler version" you use in your question. The compiler you use is configured through the compilerId and compilerVersion options and it's usually the compiler of the JDK you installed. We are talking about setting the compatibility options of your compiler.]
The properties you mention are used to provide the -source, -target and --release arguments to the javac compiler (or equivalent if you use another compiler).
Since --release was introduced in Java 9, which one applies depends on the JDK used to run maven. If you provide the --release argument:
on JDK 9 and higher, maven will ignore the -source and -target parameters,
on JDK 8 and lower, the compilation will fail miserably.
The maven-compiler-plugin specifies three ways to provide these parameters:
in the <configuration> section of a <plugin> tag. This one overrides the others,
as a property in your pom.xml. This one applies if you didn't specify the parameter in a <configuration> section,
if you didn't specify any of the above, a default for -source and -target applies (--release does not have a default).
You can find the defaults in the maven-compiler-plugin.jar (resource META-INF/maven/plugin.xml):
<configuration>
...
<release implementation="java.lang.String">${maven.compiler.release}</release>
...
<source implementation="java.lang.String" default-value="1.6">${maven.compiler.source}</source>
...
<target implementation="java.lang.String" default-value="1.6">${maven.compiler.target}</target>
</configuration>
So, since you are using version 3.8.1 of the plugin, if you don't specify anything at all, Java 6 applies.
TL;DR: if you use Java 9 and higher, your configuration options will apply in the order 3., 2., 1.
Edit: If your compiler supports it, between setting the -source/-target and --release options, I would always choose the latter. With the --release option, you won't have issues like this one. What happened there is: the source syntax was compatible with Java 8 (-source 1.8), the classes could be read by a JVM 8 (-target 1.8), but a method changed signature (in a backward, but not forward compatible way) between Java 8 and 11 and the library would not work with JRE 8.

Converting a regular Maven project to a Spring Boot project

My supervisor asked me to convert an old Maven project we have lying around into a Spring Boot project such that we are able to access the project's backend via RESTful interaction (before that the project's backend was only accessible via a console interface).
So, first I added a simple Spring Boot application in a separate package of project. After that I began to extend the pom.xml of the project by the dependencies needed for Spring Boot and adjusted the overall project setup. Now, I tried to run the backend of the old project, which turned out to be working. However, the simple Spring Boot application did not.
I narrowed down the problem to a conflicting dependency in the "old" part of the pom.xml:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.6.1</version>
</dependency>
When I leave this dependency in the pom.xml the old backend works, but the Spring Boot application fails with the following error:
WARN: Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
If I comment this dependency out the Spring Application works completely fine, but the old backend fails. I use the version 2.0.4.RELEASE of spring-boot-admin-starter-server. I think that the old backend's version of the logging package is different from the one included in spring-boot-admin-starter-server. However, I somehow need both versions in my project.
What's not possible:
Updating the old sources, since some of them have a coyright of an
external company
What I already tried, but I wasn't successful with:
Exclude the logging from then Spring Boot depedencies. This results in the following error:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
I also to tried to work with the shade plugin as some suggested from my web research. Unfortunately, I was not able to solve the problem with this approach.
Does anyone have suggestions how to solve this problem? I would be very grateful. I am not used to solve dependency problems of this kind. Please excuse me, if I am missing something obvious.
-lema
EDIT pom.xml (unfortunately I had to leave out bigger parts of it) :
<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>
...
<packaging>jar</packaging>
<description></description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-boot-admin.version>2.0.2</spring-boot-admin.version>
<spring-boot-dependencies.version>2.0.4.RELEASE</spring-boot-dependencies.version>
...
<rat.skip>true</rat.skip>
<log4j-version>2.6.1</log4j-version>
</properties>
<repositories>
...
</repositories>
<dependencyManagement>
<dependencies>
<!-- Necessary dependency for running Spring Boot without starter parent -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-releasetrain</artifactId>
<version>Fowler-SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot-dependencies.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
...
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>${spring-boot-admin.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
...
<!-- TODO The version of this dependency lets Spring Boot fail, but is
necessary tu run the old backend -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j-version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j-version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-iostreams</artifactId>
<version>${log4j-version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
<version>${log4j-version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>${log4j-version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jul</artifactId>
<version>${log4j-version}</version>
</dependency>
<dependency>
<groupId>org.fusesource.jansi</groupId>
<artifactId>jansi</artifactId>
<version>1.13</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
...
</dependencies>
<build>
<defaultGoal>verify</defaultGoal>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
...
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>prepare-config-zip</id>
<phase>prepare-package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>${basedir}/src/main/assembly/config.xml</descriptor>
</descriptors>
<finalName>configs</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution>
<execution>
<id>prepare-dist-zip</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptor>src/main/assembly/dist.xml</descriptor>
<finalName>...</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
<configuration>
<rules>
<requireJavaVersion>
<version>1.8</version>
</requireJavaVersion>
</rules>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>attach-standalone</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>standalone</shadedClassifierName>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>dont-attach-standalone</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<configuration>
<!-- Prevent huge shaded artifacts from being deployed to Artifactory -->
<outputFile>...</outputFile>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
EDIT: I just found out that if you remove the version element inside of the conflicting dependency the Spring Boot Application works, but unfortunately the backend then fails.
So I found a solution, but that's probably not the best way to do it:
I just replaced the <spring-boot-dependencies.version>2.0.4</spring-boot-dependencies.version> with an older version that is compatible with the conflicting logging dependency, namely version 1.4.7.RELEASE.
This is the latest version at which both the Spring Boot application and the backend are working simultaneously (found that out by try-and-error).
Anyway, thank you very much for your help.
Cheers

ClassNotFoundException when running fat JAR configured by spring-boot-maven-plugin

I am trying to create executable JAR file for my spring boot application.
To achieve this spring-boot-maven-plugin has been used with main class specified and packaging to jar.
Unfortunately after running output JAR file i receive java.lang.ClassNotFoundException pointing to my main class.
Below is my pom.xml and main class. Thanks in advance for help.
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.test</groupId>
<artifactId>interflight</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>interflight</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<feign-gson.version>7.2.1</feign-gson.version>
<spring-cloud.version>Edgware.SR1</spring-cloud.version>
<gson.version>2.8.0</gson.version>
<assertj-core.version>3.8.0</assertj-core.version>
<startClass>com.test.interflight.configuration.InterflightApplication</startClass>
</properties>
<dependencies>
<!--External dependencies-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>com.netflix.feign</groupId>
<artifactId>feign-gson</artifactId>
<version>${feign-gson.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--Test dependencies-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<finalName>${project.artifactId}</finalName>
<layout>ZIP</layout>
<mainClass>com.test.interflight.configuration.InterflightApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
Output error:
$ java -jar interflight.jar
Exception in thread "main" java.lang.ClassNotFoundException: com.test.interfl
ight.configuration.InterflightApplication
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(Laun
chedURLClassLoader.java:94)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner
.java:46)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
at org.springframework.boot.loader.PropertiesLauncher.main(PropertiesLau
ncher.java:587)
Main class:
#SpringBootApplication
#ComponentScan(basePackages = {"com.test.interflight.controller", "com.test.interflight.implementation.*"})
#EnableFeignClients(basePackages = {"com.test.interflight.api.restclient"},
defaultConfiguration = FeignConfiguration.class)
public class InterflightApplication {
public static void main(String[] args) {
SpringApplication.run(InterflightApplication.class, args);
}
}
Short answer:
Looks like you're using the ZIP layout when you should be using the JAR layout. Change <layout>ZIP</layout> to <layout>JAR</layout> in your Spring Boot maven plugin declaration.
Longer answer:
Take a look at the docs which describe the ZIP layout as:
ZIP (alias to DIR): similar to the JAR layout using
PropertiesLauncher.
And of which the PropertiesLauncher is described in the docs as:
Launcher for archives with user-configured classpath and main class
via a properties file...
Looks in various places for a properties file to extract loader settings, defaulting to application.properties... No default, but will fall back to looking for a Start-Class in a MANIFEST.MF
If you want it to be user specified in your Jar file, specify (or check and see if you have one already) in the meta-inf folder of your Jar, or specify the property yourself.
After several days dealing with various plugins, finally I could make a fat jar (spring boot) with externalize config files everywhere I wanted to be. You can use the following plugins:"config" is the path for your config files next to the jar file.
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<finalName>jarfilename</finalName>
<mainClass>com.start.YourStarter</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/log4j.xml</exclude>
<exclude>deploy/**</exclude>
<exclude>**/*.properties</exclude>
<exclude>**/*.txt</exclude>
</excludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.start.YourStarter</mainClass>
<useUniqueVersions>false</useUniqueVersions>
</manifest>
<manifestEntries>
<Class-Path>config/</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>

Maven EAR multimodule project not packaging persistence.xml

I`m working in a EAR project with Maven which has 2 modules. Images speaks louder than words, so let me show you the structure:
Parent pom project and modules
sigea-model contains model, repository and service layers (The "M" in MVC). sigea-web contains web pages and controller beans (VC) and sigea-ear is just a wrapper to package the other 2 modules in a EAR package.
Configuration files in modules
As you can see, sigea-ear has an empty META-INF folder. Both beans.xml files in sigea-model and sigea-web are just empty marker files because AFAIK, CDI by default search in all annotated classes (but this is not the problem right now). persistence.xml is a simple file which uses JTA transactions with a connection pool (which is working because I ping from the Glassfish's admin console and is successful).
Finally, when I package the application I get the following:
As you can see, there's no persistence.xml. All this came out because I deployed the application successfully but in the first click I got the Exception
javax.ejb.TransactionRolledbackLocalException: Exception thrown from bean
...
Caused by: java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName null
Here are my pom files:
pom.xml[sigea-app] (parent project)
<project ...>
<modelVersion>4.0.0</modelVersion>
<groupId>ar.edu.unt.sigea</groupId>
<artifactId>sigea-app</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<modules>
<module>sigea-model</module>
<module>sigea-web</module>
<module>sigea-ear</module>
</modules>
<build>
<pluginManagement>
...
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<!-- I suppress some lines for brevity -->
<dependencies>
<dependency>
<artifactId>sigea-model</artifactId>
</dependency>
<dependency>
<artifactId>sigea-model</artifactId>
<type>ejb</type>
</dependency>
<dependency>
<artifactId>sigea-model</artifactId>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<artifactId>sigea-web</artifactId>
<type>war</type>
</dependency>
<dependency>
<artifactId>sigea-web</artifactId>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
...
</dependencies>
</project>
pom.xml[sigea-ear]
<project ...>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ar.edu.unt.sigea</groupId>
<artifactId>sigea-app</artifactId>
<version>1.0</version>
</parent>
<artifactId>sigea-ear</artifactId>
<packaging>ear</packaging>
<dependencies>
<dependency>
<artifactId>sigea-model</artifactId>
<type>ejb</type>
</dependency>
<dependency>
<artifactId>sigea-web</artifactId>
<type>war</type>
</dependency>
<dependency>
<artifactId>sigea-web</artifactId>
<type>pom</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<configuration>
<defaultLibBundleDir>lib/</defaultLibBundleDir>
<skinnyWars>true</skinnyWars>
<modules>
<webModule>
<groupId>${project.groupId}</groupId>
<artifactId>sigea-web</artifactId>
<contextRoot>/sigea</contextRoot>
</webModule>
<ejbModule>
<groupId>${project.groupId}</groupId>
<artifactId>sigea-model</artifactId>
</ejbModule>
</modules>
</configuration>
</plugin>
</plugins>
</build>
</project>
pom.xml[sigea-web]
<project ...>
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>sigea-app</artifactId>
<groupId>ar.edu.unt.sigea</groupId>
<version>1.0</version>
</parent>
<groupId>ar.edu.unt.sigea</groupId>
<artifactId>sigea-web</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<name>sigea-web</name>
<dependencies>
<!-- Some dependencies including sigea-model -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
pom.xml[sigea-model] is not important as it just defines some dependencies for test and is configured to generate a package with the test classes, which are used in sigea-web for test purposes also.
Finally the question: What's failing in my configuration that doesn't package the persistence.xml file? If that's not the problem for the IllegalStateException with the message shown above: What are posible causes for that exception?
Thanks in advance for your answers.
I solved the problem by changing a dependency. In sigea-model I had
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.1.0.Final</version>
<scope>test</scope>
</dependency>
Which I used to manage the persistence context for my test methods. I didnt inquire very much in the Glassfish JPA provided implementation but maybe it's Eclipse Link instead of Hibernate. Apparently there are some incompatibility issue between those libraries. I movedhibernate-entitymanager` to compile scope, like this:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.1.0.Final</version>
</dependency>
And now the projects compile with no problem. It was also useful the comment made by khmarbaise, it simplified the project configuration, thank you very much.

Categories

Resources