Java and Scala not working together - java

I have 2 projects, one maven java and another maven Scala.
The scala one is a library, that I want to use in my java application.
I have the following dependencies in both :
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-compiler</artifactId>
<version>2.9.0-1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.9.0-1</version>
</dependency>
and as found on googling, this maven-scala-plugin in both apps
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<version>2.14.3</version>
<configuration>
<charset>UTF-8</charset>
<jvmArgs>
<jvmArg>-Xmx1024m</jvmArg>
</jvmArgs>
</configuration>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
<phase>compile</phase>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>testCompile</goal>
</goals>
<phase>test-compile</phase>
</execution>
<execution>
<phase>process-resources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
The source & target specified in maven for both are java 1.6 and I'm using Netbeans (tried even from command-line but no use)
I build the scala project and then mark a system dependency to it in the maven java project.
Infact, I'm using the goose parser (scala project - https://github.com/jiminoc/goose)
But whenever I run the java project/file, I get ClassNotFoundException for the parser classes.
Tried everything out there for hours now but no success.
Please help.
Also, to mention I've tried building with scala version 2.10.4 but it also has the same issue.

I build the scala project and then mark a system dependency to it in the maven java project.
System dependencies:
Dependencies with the scope system are always available and are not looked up in repository. They are usually used to tell Maven about dependencies which are provided by the JDK or the VM.
Since your Scala project isn't provided by the JDK or the VM, it isn't a system dependency. You would run into exactly the same problem if you had two Java projects. Just remove <scope>system</scope>.

Related

Java 11 Hibernate Validator module not found error

I am having trouble migrating my project to Java 11 from Java 8 with Hibernate validator.
I get the following error while attempting to build my project with maven:
[INFO] --- maven-processor-plugin:3.3.3:process (default) # maple-orm ---
[ERROR] diagnostic: ...\module-info.java:19: error: module not found: org.hibernate.validator
requires org.hibernate.validator;
The plugin in the pom for maven-processor-plugin is defined as follows:
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<version>3.3.3</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>process</goal>
</goals>
<configuration>
<processors>
<processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
</processors>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>5.4.10.Final</version>
</dependency>
</dependencies>
</plugin>
And the module-info.java looks like this:
module test.module {
...
requires org.hibernate.validator;
}
Is there something specific that I am missing in order to fix this issue with JPMS?
As per documentation from Github repository, Release 3.3.3. maven-processor-plugin supports targets is 9
--release release
Compiles against the public, supported and documented API for a specific VM version.
Supported release targets are 6, 7, 8, and 9.
I was able to get a solution to this problem following fabfas answer. I would suggest upgrading to version 4.0 of maven-processor-plugin and specify the proper plugin to run. Please also keep in mind I am using the jakarta suffixed libraries so this may require some fine tuning.
The module name is indeed org.hibernate.validator
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<version>4.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>process</goal>
</goals>
<configuration>
<processors>
<processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
</processors>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen-jakarta</artifactId>
<version>${hibernate.version}</version>
</dependency>
</dependencies>
</plugin>

How to generate JPA Hibernate Metamodel classes in IntelliJ IDEA?

I'm trying to generate Metamodel classes with Hibernate in IntelliJ idea.
I already saw oldest tutorials, but they didn't help me.
There are any other way to generate that files? The topics that I found about Metamodel support in IntelliJ are too old.
Maybe I'm doing something wrong. I'll explain what I'm doing.
I've already tried:
Enable the processing in IntelliJ settings: Settings > Build, Execution, Deployment > Compiler > Annotation Processors > [check] Enable annotation processing.
Put the maven-processor in pom.xml. (jpa.modelgen.CanonicalModelProcessor)
Rebuilt the project.
Plugin that I included in pom.xml:
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<version>1.3.5</version>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<outputDirectory>${project.build.directory}/generated-sources/metamodel</outputDirectory>
<compilerArguments>
-Aeclipselink.persistencexml=${project.basedir}/src/main/resources/META-INF/persistence.xml
</compilerArguments>
<processors>
<processor>org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor
</processor>
</processors>
</configuration>
</execution>
</executions>
</plugin>
Put following dependency in pom.xml of project.
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-jpamodelgen -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>5.3.7.Final</version>
<scope>provided</scope>
</dependency>
Then use ..path_to_root_directory_of_module_of_entities/target/classes/ directory on the build path of module/project.
It will make available all auto generated static model classes to the runtime of project.

Groovy Maven plugin, marking target stub as source directory

I am using gmavenplus-plugin, below is the configuration details
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<goals>
<goal>addSources</goal>
<goal>addTestSources</goal>
<goal>generateStubs</goal>
<goal>compile</goal>
<goal>generateTestStubs</goal>
<goal>compileTests</goal>
<goal>removeStubs</goal>
<goal>removeTestStubs</goal>
</goals>
</execution>
</executions>
</plugin>
After each time maven clean build, intellij's settings gets updated and it maker generated stub as source and test source folders. Ideally goal removeStubs and removeTestStubs should remove it from source. All examples on internet is based on above configuration only.
Am I missing something.
The way IntelliJ hooks into the GMavenPlus (and GMaven) lifecycle was by expecting specific goal names. 1.6 fixes this by renaming the goals to match IntelliJ's expectations.

Maven assembly : add different version of the same artifact

I create my application archive with the maven assembly plugin.
All the dependency present in my pom are included without any problem.
Now I need to include two or more version of the same artifact.
If in my pom I put
<dependencies>
[...]
<dependency>
<groupId>db.test</groupId>
<artifactId>my-model</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>db.test</groupId>
<artifactId>my-model</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
Of source the dependenvcy resolver remove the old version and only the 1.1.0 is packaged in the archive
I try to include the jar by using assembly xml descriptor file. And I didn't find any solution.
A possible solution will be to manually put all the needed model.jar inside a folder and tell the assembly to copy it in the archive. But I'm looking for a more configurable solution.
Any idea ?
I found a solution by using maven-dependency-plugin to copy resolved pom dependencies and additional jar.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<includeScope>runtime</includeScope>
</configuration>
</execution>
<execution>
<id>copy-model</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>my.test.pkg</groupId>
<artifactId>my-model</artifactId>
<classifier>server</classifier>
<version>1.0.3</version>
<type>jar</type>
</artifactItem>
<artifactItem>
<groupId>my.test.pkg</groupId>
<artifactId>my-model</artifactId>
<classifier>server</classifier>
<version>1.1.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
Now I just have to add the following lines in my assembly xml
<fileSet>
<directory>${project.build.directory}/lib</directory>
<outputDirectory>/lib</outputDirectory>
<filtered>false</filtered>
<includes>
<include>*.jar</include>
</includes>
<fileMode>0600</fileMode>
</fileSet>
Maven assumes it doesn't make any sense to have more than one version of a module at once. It assumes that a newer version replaces the older version. If it doesn't it is not the same module. I suggest you give the newer module a different name and make sure it has different packages to avoid choising a random module.
In general Maven tried to encourage good application design and deliberately makes it difficult to do things it has determined to be a bad idea.
Another ugly solution might be to use WAR file overlays, exploiting the fact that this mechanism pays no attention to the versions of component JAR files when applying the overlays.
I agree, different versions means replacing the older one. If we have to consume two different versions of a webservice for some business requirement. It is a good idea to generate the stubs in different packages and while adding to maven you can specify different them in groupid. This should work.

Possible for IDE's to resolve Scala classes in Java using Maven?

I have a project with mixed Java and Scala sources, following the instructions on this page, which works when running Maven from the command line.
However, people using IDEs like IDEA and Netbeans have problems resolving Scala classes in Java code (but not the other way around, thanks to the nice plugins available). Is there a way to resolve them?
Note: I can build from the command line just fine; the Scala classes are compiled before the Java classes. I just want the IDE to recognize this as well. I could create a separate module for the Scala classes to resolve this problem, but it seems like overkill to me.
Note: In IDEA, I have "Compile Scala classes first" and that still does not do the trick.
Update: Here are the versions I'm using:
scala-library 2.8.0
maven-scala-plugin 2.12
IDEA 9.0 Ultimate with latest scala plugin from plugin repos
Netbeans 6.9 with scala nightly plugin
Which versions (of Scala, the IDEs, the Scala plugins) are you using?
I had the same issues when I started using Scala 2.7 around 9 months ago. While I haven't tried a mixed project recently, my understanding was that the issues would be resolved in Scala 2.8. It may be worth trying Eclipse 3.5.2 with Scala 2.8 -- my impression is that the Eclipse plugin is keeping up with changes in 2.8 better than the other IDE plugins (but I could be wrong).
I have being trying to figure out how to work with Eclipse Indigo + Scala IDE 2.9, m2eclipse, mix of scala 2.9 + jdk1.7 without luck.
I found out that using maven eclipse plugin (mvn eclipse:eclipse) and importing the project as eclipse project (not maven project) with the below customization cleaned up the error marks.
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<executions>
<execution>
<id>scala-compile-first</id>
<phase>process-resources</phase>
<goals>
<goal>add-source</goal>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>scala-test-compile</id>
<phase>process-test-resources</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/scala</source>
</sources>
</configuration>
</execution>
<execution>
<id>add-test-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/test/scala</source>
</sources>
</configuration>
</execution>
</executions
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.8</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
<projectnatures>
<projectnature>org.scala-ide.sdt.core.scalanature</projectnature>
<projectnature>org.eclipse.jdt.core.javanature</projectnature>
</projectnatures>
<buildcommands>
<buildcommand>org.scala-ide.sdt.core.scalabuilder</buildcommand>
</buildcommands>
<classpathContainers>
<classpathContainer>org.scala-ide.sdt.launching.SCALA_CONTAINER"</classpathContainer>
<classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
</classpathContainers>
<excludes>
<exclude>org.scala-lang:scala-library</exclude>
<exclude>org.scala-lang:scala-compiler</exclude>
</excludes>
<sourceIncludes>
<sourceInclude>**/*.scala</sourceInclude>
<sourceInclude>**/*.java</sourceInclude>
</sourceIncludes>
</configuration>
</plugin>

Categories