I want to share my log4j property file with other modules within a project. I found out that maven-remote-resource plugin is one of the solution, but I have some problems using it.
The log4j property file is intended to be used only on test, so the tests on all module will reference the same log4j file.
The following is the structure of my project and submodules.
parent-module
-module_A
-src
-test
-properties
-log4j.properties
pom.xml
-module_B
-src
-main
-java
-src
-test
-java
pom.xml
-module_C
-src
-main
-java
-src
-test
-java
pom.xml
-module_D
-src
-main
-java
-src
-test
-java
pom.xml
pom.xml
The pom on "module_A" (the one to share resource, the log4j.properties) is as follows:
<build>
<resources>
<resource>
<directory>${basedir}/src/test/resources/</directory>
<includes>
<include>*.properties</include>
</includes>
</resource>
</resources>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<goals>
<goal>bundle</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>**/*</include>
</includes>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
and on the pom of parent module, I added the module_a to the dependency list and call the remote resource plugin
<dependency>
<groupId>com.myproject</groupId>
<artifactId>module_A</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.5</version>
<configuration>
<resourceBundles>
<resourceBundle>com.myproject:module_A:${project.version}</resourceBundle>
</resourceBundles>
<attachToMain>false</attachToMain>
<attachToTest>true</attachToTest>
</configuration>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
When I tried to do maven clean install on the whole project, each individual module doesn't see the log4j.properties.
But if I move the remote-resource to every individual sub module pom, they can see the property.
Is there a way to share the log4 property by declaring once on the parent pom?
I would suggest to make the Module_A a default jar which contains the resources in src/main/resources...nothing needed to be declared or configured. Only put the properties into the src/main/resources directory..everything will be packaged into a ja file..
<project>
<parent>
...
</parent>
<artifactId>module_A</artifactId>
</project>
And add the above module as a test dependency like:
<dependency>
<groupId>com.myproject</groupId>
<artifactId>module_A</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
in each module which needs the configuration. Than you have the properties on the test class path and it will work...
Related
I have multi-module project with structure like:
my-project
- moduleA
- moduleB
- moduleC
pom.xml for moduleA configured like:
<profiles>
<profile>
<id>withArtifacts</id>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<dependencies>
<dependency>
<groupId>com.ekiryuhin</groupId>
<artifactId>moduleB</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.ekiryuhin</groupId>
<artifactId>moduleC</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeArtifactIds>
moduleB,moduleC
</includeArtifactIds>
<outputDirectory>
${project.build.directory}/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Then:
Add some code in classes inside moduleB and moduleC.
cd to my-project/moduleA.
Run mvn clean install -PwithArtifacts -DskipTests -am
And finally I have jar files in ${project.build.directory}/lib but they do not contain my edits from (1).
Why maven may do not rebuild dependencies before copy?
UPD:
pom.xml from moduleB:
You need build all the modules for that. Go the main project my-project and call mvn clean install. You also need to make sure that moduleA depends on moduleB and moduleC so that the build order will be correct.
My ear project has the following structure:
And my pom.xml goes like this:
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<version>2.10</version>
<configuration>
<earSourceIncludes>META-INF/*</earSourceIncludes>
<packagingIncludes>META-INF/*,**/*.war</packagingIncludes>
<version>7</version>
<modules>
<webModule>
<groupId>com.ex</groupId>
<artifactId>one</artifactId>
</webModule>
</modules>
<generateApplicationXml>false</generateApplicationXml>
</configuration>
</plugin>
What I want is to include in my ear file the contents of the folder META-INF, in a similar folder in the root of the ear file called META-INF.
I've tried multiple combinations with earSourceIncludes and packagingIncludes with no success: My ear file has the linked application .war, which is good, and a META-INF folder which doesn't have what I need, but a pregenerated MANIFEST.MF file and a maven folder with the pom instead.
I wonder if I need the earSourceIncludes at all. To be honest, I don't know why it didn't work with just the packagingIncludes parameter.
You could use the maven-resource-plugin to include the content of META-INF folder to root META-INF folder. See below.
Let
Project base directory - ${project.basedir}
EAR root directory - ${project.rootdir}
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${project.build.finalName}/META-INF</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/META-INF</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
I have a Maven project where I want to have two build artifacts:
The jar file containing the compiled Java source.
A folder containing a number of .properties file.
How can I setup my Maven project to do this? And then, once I've done this, how can I consume them up the dependency graph?
Add a copy-resources goal of the Maven Resources Plugin to your POM.
<project>
...
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy-property-files</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/property-files</outputDirectory>
<resources>
<resource>
...
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</build>
...
</project>
I can't understand what you mean exactly by "consume them up the dependency graph".
I have following project structure:
Project "parent-project" does not have any source file and has subprojects as "junit-wrapper","child1-test" and "child2-test".
Subproject "junit-wrapper" has only java source inside src/main/java and this is basically created to wrap all the dependencies and binaries under the hierarchy "parent-project".
Subproject "child1-test" and "child2-test" has no source files and only contains subprojects as "child1-env" and "child2-env".
Subproject "child1-env" and "child2-env" has only junits inside src/test/java.
I want to build a super jar(within junit-wrapper) by building parent pom.xml
I hope this is possible by using maven-assembly-plugin but don't know how to configure this in pom.xml. what should be my pom.xml or assembly.xml(on using plugin) entries in order to ensure this is achieved?
please suggest.
thanks.
To create a jar which contains test-classes the best solution is to use the maven-jar-plugin like this:
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
In other modules you can use the test-jar via the following dependency:
<project>
...
<dependencies>
<dependency>
<groupId>com.myco.app</groupId>
<artifactId>foo</artifactId>
<version>1.0-SNAPSHOT</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
</dependencies>
...
</project>
You will get your "uber-jar" when you include this configuration into the pom file of junit-wrapper:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
An assembly descriptor (assembly.xml) is not necessary because the jar-with-dependencies descriptor is already available within the maven-assembly-plugin. Note that you should not execute the assembly plugin before the package phase. Otherwise the code of your maven module will not get packaged into your assembly.
I have an EAR file built by maven and currently I have a commons project which I'd like to put in an EAR\lib, so I used the maven-ear-plugin with the 'bundleDir' tag which works fine, only that now my commons.jar appears in both the lib folder and the EAR root. How do I tell it to put it only in the lib folder?
My pom:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<configuration>
<outputDirectory>../../outputs/java</outputDirectory>
<version>5</version>
<modules>
<jarModule>
<groupId>com.sample.common</groupId>
<artifactId>common</artifactId>
<includeInApplicationXml>
true
</includeInApplicationXml>
<bundleDir>/lib</bundleDir>
</jarModule>
...
</modules>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>ear</goal>
</goals>
</execution>
</executions>
</plugin>
And here is the dependency definition:
<dependency>
<groupId>com.sample.common</groupId>
<artifactId>common</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
If your jar file is not ejb you can simply put it to ear dependency without definition into maven-ear-plugin. Otherwise try to setup scope provided to your jar inside the dependency list. This may help.