Intellij doesn't work well with multi module maven scala project - scala

I have a project with standard multi project maven with mixed scala / java modules.
When I open this project on IntelliJ it fails to recognise scala folders as sources / test-sources root folders.
.
|-- pom.xml
`-- src
|-- it
| |-- resources
| | `-- data.csv
| `-- scala #### TEST SOURCES ###
| `-- com
| `-- my
| `-- example
| `-- jetty
| `-- JettyBasedServerIT.scala
|-- main
| `-- scala #### SOURCES ###
| `-- com
| `-- my
| `-- example
| `-- jetty
| `-- JettyBasedServer.scala
`-- test
`-- scala #### TEST SOURCES ###
`-- com
`-- my
`-- example
`-- jetty
`-- MainArgumentsTest.scala
As a result, interdependencies are not recognized.
I have to go module by module and manually set the the sources folders.
Do you know how to do it automtically?

I got similar issues when I wanted to set maven submodules with scala in intelliJ. I eventually succeed with particular pom.xml. You may investigate this way.
Just for information I'll give you some extracts from my pom.xml that might be useful to you :
Parent pom.xml
<pluginManagement>
<plugins>
<!-- configure scala style -->
<plugin>
<groupId>org.scalastyle</groupId>
<artifactId>scalastyle-maven-plugin</artifactId>
<version>0.8.0</version>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<verbose>false</verbose>
<failOnViolation>true</failOnViolation>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
<failOnWarning>false</failOnWarning>
<sourceDirectory>${basedir}/src/main/scala</sourceDirectory>
<testSourceDirectory>${basedir}/src/test/scala</testSourceDirectory>
<outputFile>${project.basedir}/target/scalastyle-output.xml</outputFile>
<outputEncoding>UTF-8</outputEncoding>
</configuration>
</plugin>
<!-- set scala maven plugin version -->
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<scalaCompatVersion>${scala.binary.version}</scalaCompatVersion>
<scalaVersion>${scala.version}</scalaVersion>
</configuration>
</plugin>
</plugins>
</pluginManagement>
Child pom.xml
<build>
<plugins>
<!-- Scala Compiler -->
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<executions>
<!-- Run scala compiler in the process-resources phase, so that dependencies on
scala classes can be resolved later in the (Java) compile phase -->
<execution>
<id>scala-compile-first</id>
<phase>process-resources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmArgs>
<jvmArg>-Xms128m</jvmArg>
<jvmArg>-Xmx512m</jvmArg>
</jvmArgs>
</configuration>
</plugin>
<!-- Adding scala source directories to build path -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<!-- Add src/main/scala to eclipse build path -->
<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>
<!-- Add src/test/scala to eclipse build path -->
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/test/scala</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<!-- Scala Code Style, most of the configuration done via plugin management -->
<plugin>
<groupId>org.scalastyle</groupId>
<artifactId>scalastyle-maven-plugin</artifactId>
<configuration>
<configLocation>${project.basedir}/../../tools/maven/scalastyle-config.xml</configLocation>
</configuration>
</plugin>
Unfortunately I couldn't say exactly which part is fondamental. Hope it could help.

Weird enough the problem was related to maven importer memory issue.
I increased the import memory settings and re-imported the project and it worked like magic.

Related

scala-maven-plugin mixed compile does not include src/main/java and can not find java class

pom.xml:
<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
<executions>
<execution>
<id>default-compile</id>
<phase>compile</phase>
</execution>
</executions>
</plugin>
<plugin>
<!-- see http://davidb.github.com/scala-maven-plugin -->
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
<configuration>
<args>
<arg>-dependencyfile</arg>
<arg>${project.build.directory}/.scala_dependencies</arg>
</args>
</configuration>
</execution>
</executions>
</plugin>
Code structure:
src/main/java
Hello.java
src/main/scala #will reference the class under /src/main/java
App.scala
IDE : Intellij IDEA 2017.2.1 , JDK: java8
Issues :
when ever i run the maven compile via the intellij, it always show below errors, which means it can not find the Hello.class .
Questions:
why this pom.xml does not work ? I checked the doc of scal-maven-plugin, the layout should work, but it did not .
I found it will work if i add the src/main/java as source directory via the build-helper-maven-plugin. This may explain the first question, but i realized that before the maven compile, i run the App.scala via the Intellij , so the Hello.java has already been compiled to class and i could see it under the src/main/target/classes . So Why the scala-maven-plugin can not find the class under src/main/target/classes ?
like in the documentation/sample linked in the question, remove
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>
or set them to src/.../java (the default values), no need to use the build-helper-maven-plugin
or place *.java and *.scala under the same directory (my favorite)
In dual mixed java/scala (scala depends on java and java depends of scala), the scala compiler run against java source, not from binary.
If you want to "notify" the IDE that scala source are under src/.../scala "add"
<goal>add-source</goal>
see add-source

How to build a Maven project with Eclipse with a pom.xml that imports an external properties file?

I have looked around from various questions on stackoverflow, but I have not found the answer that solve my purpose.
I want to import a properties file in a pom.xml; my purpose is to replace the <properties> section with the properties loaded from the external file.
Each property refers to the version of a particular maven dependency.
I have tried the properties-maven-plugin, but the properties are not solved and the project is not built.
I'm looking for a way that preserve the standard build of Eclipse, and also the mvn install goal of Maven.
As an example, I want that this section:
<properties>
<dependency1.version>1.0.0</dependency1.version>
</properties>
will be replaced with the property declared in a dependency.properties file, like this one:
dependency1.version=1.0.0
How can I implement my pom.xml in order to obtain this behaviour?
Thanks in advance.
EDIT
This is what I have tried:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>${basedir}/dependencies.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>my version: ${dependency1.version}</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Well, if I do a simple mvn install, in maven console it prints:
[INFO] Executing tasks
[echo] my version: 1.0.0
[INFO] Executed tasks
So, the plugin solve the property, but when I try to import the dependency with this:
<dependencies>
<dependency>
<groupId>dependency1</groupId>
<artifactId>dependency1</artifactId>
<version>${dependency1.version}</version>
</dependency>
</dependencies>
I obtain this error:
[ERROR] [ERROR] Some problems were encountered while processing the POMs:
[ERROR] 'dependencies.dependency.version' for dependency1:dependency1:jar must be a valid version but is '${dependency1.version}'. # line 21, column 13
#
The property is solved in the maven-antrun-plugin, but if it is used in the dependencies section, it doesn't work.

Maven modules will not find dependencies with scalatest

While working with scalatest I ran into a weird problem. I have a maven project with multiple modules. If I execute mvn test directly in the module. It works without problems, but if I do it in the root folder it complains about missing packages (dependencies) while compiling.
My configuration looks like following:
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_${scala.shortversion}</artifactId>
<version>3.0.0-SNAP2</version>
</dependency>
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest-maven-plugin</artifactId>
<version>1.0</version>
</dependency>
plugin configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.7</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.scalatest</groupId>
<artifactId>scalatest-maven-plugin</artifactId>
<version>1.0</version>
<configuration>
<reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
<junitxml>.</junitxml>
<filereports>WDF TestSuite.txt</filereports>
</configuration>
<executions>
<execution>
<id>scala-test</id>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
If I remove from maven-scala-plugin the goals it'll compile but scalatest will not find the test sources and exit with No tests were executed.:
<plugin>
<version>2.15.2</version>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<scalaVersion>${scala.version}</scalaVersion>
</configuration>
</plugin>
Any ideas on what I am doing wrong?!
Cheers
Hard to say without seeing all your poms, but generally speaking:
Define in root Pom all your dependencies in dependencyManagement section, your plugins in pluginManagement section with their configuration. In child poms define only those plugins and dependencies you need.
Keep in mind that xxxManagement sections only build shared definition, kind of root configuration. You still have to define in plugins and dependencies sections what you need but you can omit version and configuration elements.
This will allow all your child poms to run tests in all modules.

Packaging Maven multi-module project

I have a multi-module project that looks like this:
Module1
Sub-moduleEar
Sub-moduleJar
Sub-moduleEJB
Sub-moduleWar
Module2
Sub-moduleEar
Sub-moduleJar
Sub-moduleWar
Sub-moduleEJB
Is it possible to package Module1 and Module2 together in one file for deployment?
You could use maven dependency plugin
You have to add the lines of codes below to the pom of all submodules.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-artifact</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<type>${project.packaging}</type>
</artifactItem>
</artifactItems>
<outputDirectory>../Main/target/dependencies</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
See this.It would be helpful.

Maven assembly plugin produces undeployable war file

I'm working on a multi module project using Spring, Hibernate, GWT and Maven 3 and try to deploy a war-file distribution to a servlet container (preferably Tomcat 7 or Jetty 8). I create the war-file using maven-assembly-plugin 2.2.1 (I'm running maven 3). During the maven build everything works perfectly and the distribution archive is created.
But when I try to deploy the war file, I get ClassNotFound-Exceptions (e.g. Spring ContextLoaderListener and the like) although everything is in place (Web-inf/lib etc. pp.). So, the webapp isn't starting. Then I unpack the war-file into the servlet containers webapp directory and everything is working fine... ??
After further investigation I came across the following thing:
If I take an arbitrary war file A' created by maven-war-plugin (!), replace its content with the unpacked content from the war file I created using maven-assembly-plugin (let me call it
A.) I can see two things happen:
the resulting war file A' is a few bytes smaller than my original file A although their content is identical
the deployment of A' suddenly works like a charm
This is weird and I have absolutely no idea what's happening. Maybe there is an issue and maven-war-plugin and maven-assembly-plugin handle war file packaging differently?! I mean, a war is only a renamed zip file with some predefined structure... Maybe it has absolutely nothing to do with maven but with file encoding or other things? Any ideas on this? I appreciate any hint, that could help me in investigating this...
This is my assembly descriptor
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>dist</id>
<formats>
<format>war</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<unpack>true</unpack>
<includes>
<include>mypackage.client:parametermgmt:*</include>
<include>mypackage.static:client:*</include>
</includes>
<useProjectArtifact>false</useProjectArtifact>
<outputDirectory>/</outputDirectory>
</dependencySet>
<dependencySet>
<unpack>false</unpack>
<includes>
<include>mypackage.server.database:domain:*</include>
<include>mypackage.server.businessobjects:BOdomain:*</include>
<include>mypackage.server.security:security:*</include>
<include>mypackage.server.services:paramgmt:*</include>
</includes>
<useProjectArtifact>false</useProjectArtifact>
<outputDirectory>Web-inf/lib</outputDirectory>
</dependencySet>
<dependencySet>
<unpack>true</unpack>
<includes>
<include>mypackage.static:server:*</include>
</includes>
<useProjectArtifact>false</useProjectArtifact>
<outputDirectory>/</outputDirectory>
</dependencySet>
<dependencySet>
<unpack>false</unpack>
<includes>
<include>*</include>
</includes>
<excludes>
<exclude>mypackage.*</exclude>
</excludes>
<scope>runtime</scope>
<useProjectArtifact>false</useProjectArtifact>
<outputDirectory>Web-inf/lib</outputDirectory>
</dependencySet>
</dependencySets>
The maven-assembly-plugin is not intended to create a war. For this purpose use the maven-war-plugin instead.
If you need to create war files for different environments like test,qa, production. This can be used as well a base for different app servers. But if we are talking about a application server this means we should create ear files instead of war files (This means to use maven-ear-plugin instead of maven-war-plugin).
|-- pom.xml
`-- src
|-- main
| |-- java
| |-- resources
| |-- environment
| | |-- test
| | | `-- database.properties
| | |-- qa
| | | `-- database.properties
| | `-- production
| | `-- database.properties
| `-- webapp
You need the following assembly for that (for every environment) which can be created based on this as a template:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>test</id>
<formats>
<format>war</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<unpack>true</unpack>
<useProjectArtifact>true</useProjectArtifact>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<outputDirectory>WEB-INF</outputDirectory>
<directory>${basedir}/src/main/environment/test/</directory>
<includes>
<include>**</include>
</includes>
</fileSet>
</fileSets>
</assembly>
To support different artifacts for different environments this is the time for the maven-assembly-plugin like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>test</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>${project.basedir}/src/main/assembly/test.xml</descriptor>
</descriptors>
</configuration>
</execution>
<execution>
<id>qa</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>${project.basedir}/src/main/assembly/qa.xml</descriptor>
</descriptors>
</configuration>
</execution>
<execution>
<id>production</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>${project.basedir}/src/main/assembly/production.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
The result is creating three different war files which can be distinguished by their appropriate classifier with a single call (detailed description can be read here):
mvn clean package