We have an application.properties file in one project:
src
|--- main
|--- resources
|--- application.properties
It contains some Maven project property #variables# to initialise some properties such as:
info.app.branch=#branch.name#
As I understand through some experimentation and the above documentation, these are replaced by spring-boot-starter-parent, before it copies them over into target/classes/config.
In a dependent project, we explicitly copy the application.properties file over from the parent project's JAR using the Maven dependency plugin.
<artifactItem>
<groupId>...</groupId>
<artifactId>...</artifactId>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<includes>config/application.properties</includes>
</artifactItem>
As such, it's already in the target/classes/config directory, and no replacement of the #variables# takes place.
How can I ensure that Spring boot correctly replaces these variables?
Feels a bit awkward but you could probably specify the resource directory of the "dependent project" as the outputDirectory.
E.g:
<artifactItem>
<groupId>...</groupId>
<artifactId>...</artifactId>
<outputDirectory>${project.basedir}/src/main/resources</outputDirectory>
<includes>config/application.properties</includes>
</artifactItem>
This way I would expect the Spring Boot Maven plugin to treat them as regular properties of the project and properly replace the variables.
The way we solved this was to move the configuration files to a directory with a different name (we just called it 'configs'). The maven dependency plugin is then not unpacking directly into config:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>process-tools-resources</id>
<phase>process-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>the-parent</groupId>
<artifactId>the-artifact</artifactId>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<includes>configs/application.properties</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
We then use the Maven resources plugin to copy from configs into config, which is able to replace the #properties# for us:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<execution>
<id>process-properties-placeholders</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<delimiters>
<delimiter>#</delimiter>
</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
<resources>
<resource>
<directory>${project.build.directory}/classes/configs</directory>
<filtering>true</filtering>
</resource>
</resources>
<outputDirectory>${project.build.directory}/classes/config</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Related
Is there a way that I can get maven to only include specific .class files when importing dependencies into uber jar (shade). I'm looking for a way to get files that contain "Client" in their name to be pulled out of the dependency jars and added to the final jar. Any help would be wonderful.
You should be able to use the maven-dependency-plugin like this:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>unpack</id>
<phase>package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId><!--dependency groupId--></groupId>
<artifactId><!--dependency artifactId--></artifactId>
<version><!--depedency version--></version>
<includes>**/*Client*.java</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
If you are using the Maven Shade Plugin, you can a filter, which will allow you to filter which artifacts get shaded, but as well as which classes to exclude or include.
Here's the example they provide:
<filters>
<filter>
<artifact>junit:junit</artifact>
<includes>
<include>org/junit/**</include>
</includes>
<excludes>
<exclude>org/junit/experimental/**</exclude>
</excludes>
</filter>
</filters>
I have a situation where i need to Unpack a spring jar and replace a file in that package with my .class file and repackage it back as jar. I understood that this can be done in Maven. So i created a maven project and i added the .java(to be replace in the Spring jar) to it . Here is my Project structure.
here is my pom.xml
<project>
<modelVersion>4.0.0</modelVersion>
<name>repackage</name>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.4.6</version>
<packaging>pom</packaging>
<build>
<plugins>
<!-- Unpacking -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>unpack</id>
<phase>package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6.SEC03</version>
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>target</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<!-- Packing -->
<!-- <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classesDirectory>target</classesDirectory>
<classifier>sample</classifier>
</configuration>
</execution>
</executions>
</plugin>-->
</plugins>
</build>
</project>
I am able to unpack the spring jar using maven dependency plugin and i figured out how to pack it back to a jar using maven-jar-plugin. The part i am struggling to understand 1) how convert my .java file into a .class file. As the packaging is pom type <packaging>pom</packaging> I am not able to compile the java file(JdkVersion.java). if successful in creating the class file 2) How to replace this .class file into unpacked spring jar as the file i need to replace is inside the sub directory of /springpackage
You don't have to. Spring is an open source project just clone its source code https://github.com/spring-projects/spring-framework?files=1 and update the code and create a package using simple mvn package command
I am trying to unpack a maven dependency jar's contents into my classes folder, and at the same time include the transitive dependencies. I also don't want to unpack all of my project's dependencies. Only one would be good, even better if I could do this to a list of them. Found similar solutions but nothing addressing my exact issue.
Example Main Project Pom:
.
.
.
<dependencies>
<dependency>
<groupId>com.test.dep</groupId>
<artifact>first-dependency</artifact>
</dependency>
<dependency>
<groupId>com.test.dep</groupId>
<artifact>second-dependency</artifact>
</dependency>
</dependencies>
.
.
.
Example second-dependency Pom:
.
.
.
<dependencies>
<dependency>
<groupId>com.test.dep</groupId>
<artifact>third-dependency</artifact>
</dependency>
<dependency>
<groupId>com.test.dep</groupId>
<artifact>fourth-dependency</artifact>
</dependency>
</dependencies>
.
.
.
I want second-dependency to be unpacked into my classes folder nested under target and also want any of the artifacts (third-dependency, fourth-dependency) it depends on to still be included in my lib folder (not unpacked).
I tried the following (without including the the artifact in my dependencies):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>generate-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.test.dep</groupId>
<artifactId>second-dependency</artifactId>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<includes>**/*</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
And this did include the contents of second-dependency in my classes folder, but did not include third-dependency or fourth-dependency in my main projects lib directory.
Any ideas?
Try to use following plugin configuration, based on the described parameters of dependency:unpack-dependencies, instead:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>generate-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<includeArtifactIds>second-dependency</includeArtifactIds>
</configuration>
</execution>
</executions>
</plugin>
Im not sure about your use case.
But if you want to build jar, that sounds like a use case for the maven-shade-plugin. This plugin is able to package the classes and resources of the project itself as well as of a specified set of artifacts (dependencies) into one jar.
Just define the artifact itself ("${project.groupId}:${project.artifactId}") and the "second dependency" to be included.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<configuration>
<artifactSet>
<includes>
<include>${project.groupId}:${project.artifactId}</include>
<include>com.test.dep:second-dependency</include>
</includes>
</artifactSet>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
I have maven prj with 2 modules: a JAR and a WAR that depends on the JAR.
after the MAVEN INSTALL I have into a WAR the classic WEB-INF/lib folder that contains all the jar dependencies(including the first module's JAR).
I need the first module's JAR is moved to another folder, for example WEB-INF/resources.
as I can?
I was able to move the jar but only within the TARGET, the WAR remains the same.
I used the following plugin:
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.9</version>
<executions>
<execution>
<id>copy-installed</id>
<phase>install</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>groupId</groupId>
<artifactId>artifactId</artifactId>
<version>1.0</version>
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}/.../WEB-INF/services</outputDirectory>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}/.../WEB-INF/services</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
For now, I surrendered.
I could not move this JAR from WEB-INF/lib to WEB-INF/services of the EAR.
The workaround I used is to say MODULE1 to copy its JAR in the WEB-INF/services of MODULE2. Consequently, the JAR will be present in the WEB-INF/services of the sources folders, in the WEB-INF/services of the TARGET folders and in the end in the WEB-INF/services of the EAR.
i used:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
<configuration>
<outputDirectory>..MOSULE1/src/main/webbapp/WEB-INF/services</outputDirectory>
</configuration>
</plugin>
</plugins>
into MODULE1 pom.
I had to give up to make module1 independent module2 ;(
i hope i will found a clean solution..
Maybe this will help:
<project.warSourceDirectory>${basedir}/src/main/webapp</project.warSourceDirectory>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.9</version>
<executions>
<execution>
<id>copy-installed</id>
<phase>install</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>groupId</groupId>
<artifactId>artifactId</artifactId>
<version>1.0</version>
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>${project.warSourceDirectory}/WEB-INF/services</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
What is the best way to put javascript/html/css code in the maven repository, so that is easily usable by java projects.
Is there a way to do it such that the included project can be easily made "web-visible" by the including project?
For example assume I write a very useful tricks.js file an put it in the mvn repository.
Is it possible to create a web project that adds tricks.js as a dependency and then doing
<script src="/some/thing/tricks.js"/>
causes the tricks.js file to be served?
External resources should be packaged into an artifact and published to the repository (for simplicity use a jar artifact, but you could specify an assembly to package a zip instead to make it clear what the artifact is for). The maven-dependency-plugin unpacks the jar's contents into a specified location in the war project. That folder is then specified as an external web resources directory for the maven-war-plugin.
The following configuration will unpack the contents of my-resources into the target/external-resources directory, then include the contents of that folder in the war as if they'd been defined in src/main/resources.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>generate-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>name.seller.rich</groupId>
<artifactId>my-resources</artifactId>
<version>1.0.0</version>
<type>jar</type>
<overWrite>false</overWrite>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}/external-resources</outputDirectory>
<overWriteReleases>false</overWriteReleases>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.0</version>
<configuration>
<webResources>
<resource>
<directory>${project.build.directory}/external-resources</directory>
</resource>
</webResources>
</configuration>
</plugin>
You can pack it into jar and then unpack it by maven plugins.