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

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.

Related

scala-maven-plugin and Scala

I have a scala project that depends on 2.11.8. Hence that is the version I am declaring as a dependency in my project. I have configured the maven-scala-plugin as follows.
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-compiler</artifactId>
<version>2.11.8</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<sourceDir>src/main/scala</sourceDir>
<testSourceDir>src/test/scala</testSourceDir>
<scalaVersion>${scala.version}</scalaVersion>
<jvmArgs>
<jvmArg>-Xms64m</jvmArg>
<jvmArg>-Xmx1024m</jvmArg>
</jvmArgs>
</configuration>
</plugin>
However, when I build my project I receive:
[ERROR] Failed to execute goal org.scala-tools:maven-scala-plugin:2.11:compile (default) on project myproject: wrap: org.apache.maven.project.InvalidProjectModelException: 1 problem was encountered while building the effective model
[ERROR] [FATAL] Non-parseable POM /Users/myuser/.m2/repository/org/scala-lang/scala-compiler/2.11.8/scala-compiler-2.11.8.pom: entity reference name can not contain character =' (position: START_TAG seen ....uk/main?ParticipantID=nmum6rqpq5q6gkm3933n3nf9s76onu6r&FailedURI=... #1:265) # line 1, column 265
Let me know if there is any additional information that would help resolve this issue.
Thanks in advance!
Dependencies for scala in .m2 were corrupt - cleaned them out and did a clean install for everything. All fixed!

Dynamic maven artifactId

Is it possible for a POM to declare (or at least publish) an artifactId containing system properties? I mean the artifactId of the actual project, not dependencies.
I am using maven to build a scala project and thus, to allow publishing the project for different scala versions, in the pom.xml I'd like to declare:
<artifactId>myproject_${scalaBinaryVersion}</artifactId>
however maven 3.3. complains
[WARNING] 'artifactId' contains an expression but should be a constant
Since I'd like this project to be interoperable with sbt, what would be the best way to publish an artifact suffixed with the scala binary version?
The Maven way of doing so would be to use classifiers. From official documentation an example matches exactly your case (for different Java versions, but you can replace Java with Scala):
The classifier allows to distinguish artifacts that were built from the same POM but differ in their content. It is some optional and arbitrary string that - if present - is appended to the artifact name just after the version number.
As a motivation for this element, consider for example a project that offers an artifact targeting JRE 1.5 but at the same time also an artifact that still supports JRE 1.4. The first artifact could be equipped with the classifier jdk15 and the second one with jdk14 such that clients can choose which one to use.
You can configure your POM as following:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>${scalaBinaryVersion}</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Note: we are adding an additional execution of the Maven Jar Plugin, so the project would create two jars, the normal one + an additional one with the specified (dynamic) classifier.
Then Maven will automatically publish the classified jar together with the normal jar (since it will be automatically attached to the build). You can then import it as a further Maven dependency in another project specifying its classifier as part of the Maven GAV (GAVC in this case):
<dependency>
<groupId>your.group.id</groupId>
<artifactId>your.constant.artifact.id</artifactId>
<version>your.version</version>
<classifier>your.dynamic.classifier</classifier>
</dependency>
If you want to only build the classified one and no standard (unused) jar, you can skip the creation of the normal jar as following:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>default-jar</id>
<phase>none</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
<execution>
<id>scala-version-jar</id>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>${scalaBinaryVersion}</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Note: we are simply overriding the default execution of the Jar Plugin and binding it to a non existing phase. Hence Maven will only generate the classified Jar. The Install Plugin will then only install the classified one.
Update: how to have dynamic artifactId installed with dynamic dependencies
If different transitive dependencies are required for different dynamic versions, then indeed classifiers are not suitable.
Dynamic artifactIds with dynamic dependencies (and hence dynamic transitive dependencies) can however be achieved. Here below is the approach I used (and successfully tested):
As preference, I isolated the dynamic behavior in a profile, but you can obviously move it back to the default build (or have the profile active by default).
First of all, let's define in our pom the dependencies requiring a dynamic version, hence via properties as following:
<properties>
<scalaBinaryVersion>scalaversion</scalaBinaryVersion>
<dependency.version>4.11</dependency.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${dependency.version}</version>
</dependency>
</dependencies>
Note: for the sake of an example, I'm using Junit as dependency in this case and not in test scope, because I want it as compile dependency (again, just for this example).
Then let's define a profile for our dynamic behavior:
<profiles>
<profile>
<id>build-scala-version</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
<configuration>
<finalName>${project.artifactId}_${scalaBinaryVersion}-${project.version}</finalName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy-pom</id>
<phase>generate-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${scalaBinaryVersion}</outputDirectory>
<resources>
<resource>
<directory>${basedir}</directory>
<includes>
<include>pom.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.1</version>
<executions>
<execution>
<id>replace-artifactid</id>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
<configuration>
<file>target/${scalaBinaryVersion}/pom.xml</file>
<token><artifactId>${project.artifactId}</artifactId></token>
<!-- Replace to -->
<value><artifactId>${project.artifactId}_${scalaBinaryVersion}</artifactId></value>
<outputDir>target\${scalaBinaryVersion}\replacer</outputDir>
<outputFile>pom.xml</outputFile>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<executions>
<execution>
<id>default-install</id>
<configuration>
<skip>true</skip>
</configuration>
</execution>
<execution>
<id>install-scala-version</id>
<phase>install</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}_${scalaBinaryVersion}</artifactId>
<version>${project.version}</version>
<packaging>${project.packaging}</packaging>
<file>${project.build.directory}/${project.artifactId}_${scalaBinaryVersion}-${project.version}.jar</file>
<pomFile>${project.build.directory}/${scalaBinaryVersion}/replacer/pom.xml</pomFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Note, the profile is customizing and providing the following:
Changing the final Jar name with a dynamic name, depending on the runtime (aka dynamic) value, to ${project.artifactId}_{scalaBinaryVersion}-${project.version}
Filtering the existing pom file via the Maven Resources Plugin and copying it to the directory target\${scalaBinaryVersion}. The copied pom will have the dependencies with the dynamic version because the Resources Plugin will replace them. However, it will not have the dynamic artifactId (yet).
Finalizing the dynamic pom file. The Replacer plugin will replace the artifactId XML element with the dynamic value (working on the target folder, hence everything on temporarely files)
Skipping the generation of the default installation
Performing a custom install-file installation with the dynamic pom file (the filtered, copied and replaced one, providing dynamic dependencies (and as such dynamic transitive dependencies) and a dynamic artifactId
Hence, performing the following maven invocation:
mvn clean install -Pbuild-scala-version -DscalaBinaryVersion=hello -Ddependency.version=4.4
Maven will effectively install a new artifact in the local cache for the dynamic artifactId, the dynamic dependency version and the dynamic pom.
Note: if the concerned dependency version(s) and the dynamic scala version is the same, then you can save up a parameter and make the invocation shorter and more consistent.
if you are using maven for that I will saggest using multi pom with maven helper plugin so the artifactId is constant in each module.
We follow the properties (via profiles) based approach as suggested here: scala-maven-plugin FAQ
<artifactId>something_${scala.compat.version}</artifactId>
<properties>
<scala.compat.version>2.12</scala.compat.version>
</properties>
maven will still issue a warning (with good reason), but with the help of the flatten-maven-plugin we build/install poms that has the variables replaced.

m2e shade eclipse "project main artifact does not exist"

I'm trying to make a deployment package that bundles all the dependencies of my maven module that has dependencies to another maven project in eclipse.
I have this in my pom.xml
<modelVersion>4.0.0</modelVersion>
<groupId>com.my.proj</groupId>
<artifactId>AAA</artifactId>
<version>0.0.2-SNAPSHOT</version>
<name>btc</name>
<dependencies>
<dependency>
<groupId>com.another.proj</groupId>
<artifactId>BBB</artifactId>
<version>1.4-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.group.id.Launcher1</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
I use that "m2 Maven Build" in eclipse with the goal "org.apache.maven.plugins:maven-shade-plugin:shade" and the "Resolve Workspace artifacts" ticked.
It's failing with
--- maven-shade-plugin:1.6:shade (default-cli) # AAA ---
[ERROR] The project main artifact does not exist. This could have the following
[ERROR] reasons:
[ERROR] - You have invoked the goal directly from the command line. This is not
[ERROR] supported. Please add the goal to the default lifecycle via an
[ERROR] <execution> element in your POM and use "mvn package" to have it run.
[ERROR] - You have bound the goal to a lifecycle phase before "package". Please
[ERROR] remove this binding from your POM such that the goal will be run in
[ERROR] the proper phase.
I ran out of idea at this point.
[ERROR] The project main artifact does not exist. This could have the following
We were getting this problem recently. What resolved it for us was to not do mvn shade:shade but instead use:
mvn package
This does additional compilation and package work before running the shade plugin and so the main class was available on the classpath.
The shade plugin is attempting to include the project's artifact in the shaded JAR. Since it doesn't exist (yet), you're getting this error. You either need to build/package the project artifact first (e.g., by attaching the shade goal to the package phase)
If you don't have any project artifact to include in the shaded JAR, you can add an excludes node to remove the project's artifact.
Here's an example:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.group.id.Launcher1</mainClass>
</transformer>
</transformers>
<excludes>
<exclude>com.my.proj:AAA</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>

gwt-maven-plugin how to add the source jar in another module pom.xml

my application has 2 module, one is jar and the other is gwt war. in the jar module (non-gwt) pom.xml, I add
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
and als-admin-viewer-core-1.0.0-sources.jar is successfully created.
Then In the webapp(a gwt application) pom.xml, I want to use this jar, and in the segment, I add
<plugin>
<groupId>org.codehaus.mojo<groupId>
<artifactId>gwt-maven-plugin</artifactId>
<version>2.4.0</version>
<dependencies>
<dependency>
<groupId>hk.gov.ehr.service.tch.als</groupId>
<artifactId>als-admin-viewer-core</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>hk.gov.ehr.service.tch.als</groupId>
<artifactId>als-admin-viewer-core</artifactId>
<version>1.0.0</version>
<classifier>sources</classifier>
</dependency>
</dependencies>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>compile</goal>
</goals>
</executions>
........
but when I run maven install for this project (als-admin-viewer-webapp), error
No source code is available for type hk.gov.ehr.service.tch.als.admin.viewer.core.LogSearchCriteria; did you forget to inherit a required module?
is prompted.
what is the problem?!!
I even try to add
<compileSourcesArtifacts>
<compileSourcesArtifact>hk.gov.ehr.service.tch.als:als-admin-viewer-core</compileSourcesArtifact> <!-- groupId:artifactId -->
</compileSourcesArtifacts>
in
<configuration>
section of gwt-maven-plugin, but it still does not help!!
I think you forgot to create "gwt.xml" file in your jar module and inherit it in your main gwt.xml (inside gwt maven module).
Please look at
http://mojo.codehaus.org/gwt-maven-plugin/user-guide/library.html
for details (section "Using general purpose JARs as GWT library").
Also bear in mind: if you're using maven-source-plugin to attach sources, your sources will be distributed with the web application. And if you're using "compileSourcesArtifacts" you avoid this side-effect.

Maven Javadoc - Unable to generate Javadoc

I have the following dependency and build in my pom file. I'm able to manually create the javadoc with a Maven command. I can also succesfully perform a build. The output doesn't mention javadoc at all. I've also tried leaving out the output directory paths. POM File
Dependency section:
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.8</version>
</dependency>
and then the build section:
<build>
<finalName>D</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.8</version>
<configuration>
<outputDirectory>${project.build.directory}/javadoc</outputDirectory>
<reportOutputDirectory>${project.reporting.outputDirectory}/javadoc</reportOutputDirectory>
<version>2.8</version>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>aggregate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
The Maven Javadoc plugin doesn't run by default and needs to be bound to one of the default Maven lifecycle phases.
Here's how I would write the plugin's configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.8</version>
<configuration>
<outputDirectory>${project.build.directory}/javadoc</outputDirectory>
<reportOutputDirectory>${project.reporting.outputDirectory}/javadoc</reportOutputDirectory>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<phase>site</phase>
<goals>
<goal>aggregate</goal>
</goals>
</execution>
</executions>
</plugin>
Notice how I added an extra phase element to the execution. This will bind it to the "site" goal so that javadocs are generated when you run mvn site. Check Introduction to the Build Lifecycle if you want one of the default Java build phases.
Also note that I ditched the version parameter; by default, it should use your POM's version anyway.