how to access main/resources of a maven dependency lib? - java

got a maven submodule project including set of dlls in main\resoursces. How can I access from my other module project ?
The maven profile detects guest os and download dlls submodule project similar to Resolving a Maven dependency differently if the JVM in use is x86 or x64?

You can introduce a resource in your pom.xml file
<project>
<build>
<resources>
<resource>
<directory>${Submodule_path}</directory>
<includes>
<include>*.dll</include>
</includes>
<targetPath/>
</resource>
</resources>
</build>
</project>
Other option is to use "build-helper-maven-plugin" and introduce there the source that you want.

Related

Maven filtering - replacement of variables

I have a maven project, which generates a jar file as a web project. Based on Maven I include a standalone Tomcat. Inside of the jar file, there is actually the war-file, which contains my application.
This application contains a "version.txt" in src/main/config (or any similar path), that is finally included in the war-file.
This version.txt looks like:
version: ${project.version}
I would like, that maven should replace the variable with the correct version from pom.xml.
In my pom.xml I have included:
<build>
<resources>
<resource>
<directory>src/main/config</directory>
<filtering>true</filtering>
<includes>
<include>**/version.txt</include>
</includes>
</resource>
<resource>
<resources>
</build>
So is there any way to include this version.txt and a working replacement in a war-file, which is in a (Tomcat)jar-file?
Addendum:
My File hierarchy looks like:
jar-file
-- ...
--war-file
---- ...
----version.txt
I suggest you use the maven-war-plugin.
https://maven.apache.org/plugins/maven-war-plugin/usage.html
It will expect a certain directory layout, and in the examples it clearly shows how to filter (replace maven variables into the web resources)
https://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering-webresources.html
If this solution falls short, then a more specific question based on this should should be asked later.
Thanks for the advice. No, there was no maven-war-plugin, but I have included it and based on the instructions, it works.
Short solution:
Added a resourceDirectory on same level like pom.xml and included version.txt
Added in pom.xml and activate filtering:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
<configuration>
<webResources>
<resource>
<filtering>true</filtering>
<directory>externalresources</directory>
</resource>
</webResources>
</configuration>
</plugin>

Create META-INF/services file with Maven

Is there a way to create a custom services file within META-INF/services with Maven? This is how you would do it with Ant: https://ant.apache.org/manual/Tasks/jar.html
I understand that it's possible to simply create a resources/META-INF/ in my source code and place whatever services file I want in there. Maven will then automatically pull those files into my JAR. This does not solve my issue.
The contents of my service file changes depending on the type of JAR I'm building, so I can't simply create it in my source code folders.
Having multiple versions of the service file in my source code, to have Maven exclude the ones I don't need, also doesn't solve my issue. This is because the service file needs to be a specific name; having multiple versions of the file will prevent this.
Summary: Is there a way to create the contents of a service file (META-INF/services) with Maven?
Thanks!
If you can create a reasonably low number of such service files you could store them in a separate path in your project. For example:
Then you can selectively include the files with an pom.xml like this (one more example that pom.xml is powerful, but verbose):
<properties>
<service.declaration.dir>src/serviceManifests</service.declaration.dir>
<service.files.path>META-INF/services</service.files.path>
</properties>
<profiles>
<profile>
<id>foo</id>
<build>
<resources>
<resource>
<directory>${service.declaration.dir}</directory>
<includes>
<include>${service.files.path}/foo</include>
</includes>
</resource>
</resources>
</build>
</profile>
<profile>
<id>bar</id>
<build>
<resources>
<resource>
<directory>${service.declaration.dir}</directory>
<includes>
<include>${service.files.path}/bar</include>
</includes>
</resource>
</resources>
</build>
</profile>
</profiles>
To include both files you will then run:
mvn clean package -P foo -P bar
To only include the foo file, you will run:
mvn clean package -P foo

How to add an extra source directory for maven to use only when running tests or when debugging?

I have a set of configurable plugins that are being debugged.
They would not exist in a build of the main program but are part of the debugging regimen.
How to add them as resources that can be used in tests and debugging but not included in the final build?
The tests and debugging depends on the program locating the proper plugins and configuration directory/folders, compiling sources on the fly and then testing them with also stepping through them.
As a result, the source files used during the test must reside in the normal folder used during run-time.
I have this:
<build>
<resources>
<resource>
<directory>conf</directory>
</resource>
</resources>
</build>
The problem is that the resources are now being added to the final compile, which is not what is desired.
You'll have to keep the ressources in a different folder. as mentioned in the comments, you could use the standard archiecture (src/main/resources and src/test/resources) or as you tried you can define a test resources folder:
<build>
<sourceDirectory>src</sourceDirectory>
<testSourceDirectory>test</testSourceDirectory>
<resources>
<resource>
<directory>resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>testresources</directory>
</testResource>
</testResources>
Helpfull links you'll find here: access the ressources in junit and the maven guide

How can I write maven build to add resources to classpath?

I am building a jar using maven with simple maven install.
If I add a file to src/main/resources it can be found on the classpath but it has a config folder where I want that file to go but moving it inside the config folder makes it disappear from the classpath.
A cleaner alternative of putting your config file into a subfolder of src/main/resources would be to enhance your classpath locations. This is extremely easy to do with Maven.
For instance, place your property file in a new folder src/main/config, and add the following to your pom:
<build>
<resources>
<resource>
<directory>src/main/config</directory>
</resource>
</resources>
</build>
From now, every files files under src/main/config is considered as part of your classpath (note that you can exclude some of them from the final jar if needed: just add in the build section:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>my-config.properties</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
so that my-config.properties can be found in your classpath when you run your app from your IDE, but will remain external from your jar in your final distribution).
If you place anything in src/main/resources directory, then by default it will end up in your final *.jar. If you are referencing it from some other project and it cannot be found on a classpath, then you did one of those two mistakes:
*.jar is not correctly loaded (maybe typo in the path?)
you are not addressing the resource correctly, for instance: /src/main/resources/conf/settings.properties is seen on classpath as classpath:conf/settings.properties
By default maven does not include any files from "src/main/java".
You have two possible way to that.
put all your resource files (different than java files) to "src/main/resources" - this is highly recommended
Add to your pom (resource plugin):
​
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>

Copy a file during maven build phase

My situation is:
I have a Maven project, I have my java classes in /app/src/main/java, my resources in /app/src/main/resources and my webapp files in /app/src/main/webapp
I have a javascript file in /common/script.js
Now what I want is to include (copy) the javascript file to the war file during the build phase of maven. To be precise, I want the script.js to land in /js/ directory of the war archive, just as it was placed in /app/src/main/webapp/js before starting the build.
I need this to share one version of resource files among many web-apps.
Kind regards,
Q.
You could do something like this, as documented here.
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<webResources>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>../common</directory>
<targetPath>/js</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
...
</project>
You can use the mojo copy-resources to copy resources which are not in the default maven layout or not declared in the build/resources element.
Check
"maven-resources-plugin"
You can use maven-resources plugin to copy a file to the desired location. Before or after a war has been built

Categories

Resources