A POM dependency contains native libraries (DLLs inside a JAR file). How do I programmatically look up the path of the downloaded JAR file so I can pass it into "java.library.path"?
Answering my own question: http://web.archive.org/web/20120308042202/http://www.buildanddeploy.com/node/17
In short, you can use the maven-dependency-plugin:unpack goal to extract the libraries into a known path, and pass that into java.library.path:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>compile</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.jdesktop</groupId>
<artifactId>jdic-native</artifactId>
<version>${jdic.version}</version>
<classifier>${build.type}</classifier>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
Since System.load() can't load libraries from within a jar, you will have to use a custom loader which extracts the library to a temporary file at runtime. Projects With JNI discusses this approach and provide code for the custom loader.
Library loader
We now have our JNI library on the
class path, so we need a way of
loading it. I created a separate
project which would extract JNI
libraries from the class path, then
load them. Find it at
http://opensource.mxtelecom.com/maven/repo/com/wapmx/native/mx-native-loader/1.2/.
This is added as a dependency to the
pom, obviously.
To use it, call
com.wapmx.nativeutils.jniloader.NativeLoader.loadLibrary(libname).
More information is in the javadoc for
NativeLoader.
I generally prefer to wrap such things
in a try/catch block, as follows:
public class Sqrt {
static {
try {
NativeLoader.loadLibrary("sqrt");
} catch (Throwable e) {
e.printStackTrace();
System.exit(1);
}
}
/* ... class body ... */
}
An alternative would be to unpack the dependency, for example using dependency:unpack.
You can use the maven dependency plugin to copy the artifacts to a predefined path:
http://maven.apache.org/plugins/maven-dependency-plugin/examples/copying-artifacts.html
If the DLL is inside the JAR, then you will need to copy it out to a directory before it can be loaded. (JARs that include native libraries usually do this themselves.) If your JAR isn't doing this, then you can use Class.getResourceAsStream() and write this to a directory that you've added to the java.library.path.
For an example of this, see loadNativeLibrary in JNA. It uses this technique to load it's own library (a JNI library) from a JAR.
Related
I have a maven multi module groovy project. When I run a main method in a groovy class thats in one of the submodules, intellij is not re-compiling before running it. It always runs the version last compiled when I manually initiated maven:compile. I don't recall having to do this manually or set any special intellij project settings in the past for this to work.
I've tried reimporting my project, several incarnations of updates to my poms, and then ultimately I had to create a run configuration where I specify a "Before Launch" configuration that executes mvn compile first. This just seems like a hack though. Its unclear to me if my issue is in a poorly constructed set of poms or if I'm truly missing some intellij configuration.
This works the first time
class MyClass2 {
static void main(String... args) {
print("foo")
}
}
but if I add another print statement such as printing bar, the output of the program only prints foo and not foo and bar.
class MyClass2 {
static void main(String... args) {
print("foo")
print("bar")
}
}
My module structure is like this:
my-project
module-1
src/main/groovy/com/foo/MyClass2.groovy (Depends on Module1)
module-2
src/main/groovy/com/foo/MyClass1.groovy
My pom file for this submodule has this build section:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.7.1</version>
<executions>
<execution>
<goals>
<goal>addSources</goal>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
I can add more pom config if the problem is rooted there or provide screenshots of intellij config. I suspect intellij is the root of the problem because mvn compile and mvn clean install works just file at the parent as well as all submodules. I have no issues referencing MyClass1 from MyClass2; dependencies seem to be setup correctly.
Verify that you have Build step in Before launch section of Run Configuration added:
I am deploying a war into JBoss 7.x using Arquillian for testing and it has a dependency on Apache Commons Collections. However, it just doesn't seem to pick up the module dependency.
MANIFEST.MF
Dependencies: org.apache.commons.collectionss export
Arquillian Deployment
#Deployment
public static Archive<?> createDeployment() {
WebArchive archive = ShrinkWrap.create(WebArchive.class);
archive
.addPackages(true, "com.example.package")
.addAsResource("META-INF/MANIFEST.MF", "META-INF/MANIFEST.MF")
// * Tried the following two options with no luck
//.AddAsManifestResource("META-INF/MANIFEST.MF", "MANIFEST.MF")
//.AddAsWebInfResource("META-INF/MANIFEST.MF", "META-INF/MANIFEST.MF")
// * If I enable the following, it works fine. getLibrary just picks
// * up the lib through maven.
//.addAsLibraries(
// getLibrary("commons-collections:commons-collections:3.2.1"))
;
return archive;
}
I don't want to use jboss-deployment-structure.xml since it feels like using a sledgehammer to crack a nut.
Any ideas?
In my case I added a MANIFEST.MF within src/test/resources and .addAsManifestResource("MANIFEST.MF") for Arquillian
MANIFEST.MF
Manifest-Version: 1.0
Built-By: me
Build-Jdk: 1.6.0_45
Created-By: Maven Integration for Eclipse
Dependencies: org.infinispan export
Arquillian
#Deployment(testable = false)
public static WebArchive createDeployment() {
MavenDependencyResolver mvnResolver = DependencyResolvers.use(MavenDependencyResolver.class).loadMetadataFromPom("pom.xml").goOffline();
return ShrinkWrap
.create(WebArchive.class, "example.war")
.addPackages(true, Filters.exclude(".*Test.*"), "com/comapany/")
.addAsManifestResource("MANIFEST.MF")
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
}
}
As it turned out, I asked this same question again (this time about Jar Files) since I forgot all about this question.
The solution has also since been discovered: (Same as from the other question)
The whole thing turned out to be a lot simpler.
Even with .addAsManifestResource OR .setManifest, the MANIFEST.MF was autogenerated by Maven.
This was resolved with the following section in pom.xml instead of using a custom MANIFEST.MF and using .setManifest("META-INF/MANIFEST.MF"); The MANIFEST.MF is auto-generated and there is no customised copy in the resources folder (to avoid confusion more than anything since it was ignored anyway)
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Dependencies>
org.infinispan,
org.infinispan.infinispan-tree export,
</Dependencies>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
I have a maven project which generates a jar via the maven assembly plugin I want to run as a console app. However, the MainClass attribute is not being set in MANIFEST.MF. Here is my plugin configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>net.justaprogrammer.poi.cleanser.Cleanser</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
However, this does not get added to the MANIFEST.MF in the jar generated by mvn package. The manifest generated is below:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: zippy
Build-Jdk: 1.6.0_25
What am I doing wrong?
I missed that you weren't generating your assembly on package. You have a jar project, so Maven will build a jar using the maven-jar-plugin. You don't have to have anything in your pom to tell it that. That's Maven's convention-over-configuration working for you. The jar it builds will have only your project classes and resources in it. If you want to add a Main-Class to the manifest in that jar, you should configure the jar plugin to do so. Basically, just move that archive configuration to the jar plugin.
However, if you actually want to assemble an executable fat jar--that is, a jar that includes all of your classes as well as the classes of all of your dependencies--then you have the setting in the right place, but you need to actually run the assembly plugin either using mvn assembly:single or by binding that goal to a lifecycle phase. To be clear, if you do this, then your project will output two jars: one that contains your project files and one that contains that plus the contents of all the libraries that your project depends on. The former is built by the jar plugin. That latter is built by the assembly plugin. Note that fat jars aren't commonly used, and you can run into unusual problems when you use them because they're rather outside the realm of normal Java stuff.
For copy&paste fans like me, assembled from above answer, and http://maven.apache.org/plugins/maven-assembly-plugin/usage.html#Execution:_Building_an_Assembly:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.db.search.filenet.Load</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
If you happen to be using the maven shade plugin to build a fat jar (rather than or in addition to using the assembly plugin), it's worth noting that the shade plugin handles entires in the MANIFEST.MF file a bit differently; see the shade plugin's executable jar page.
You probably need to add the maven-jar-plugin configuration too and configure the MainClass there also. The assembly unpacks all JAR files (e.g. project jar and dependency jars) and I think that the last MANIFEST.MF found in the list of JAR files "overwrites" the expected/generated manifest.mf.
I have to generate sources using wsimport and i assume that it should go to /target/generated-sources/wsimport rather than /src/main/java.
The problem is that wsimport needs target folder created before execution and it fails. Can I create that dir first using any maven plugin. I can do it using ant but i prefer to keep it in POM.
Try using the add source goal of the build helper plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/target/generated/src/wsimport</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
I have to generate sources using wsimport and i assume that it should go to /target/generated-sources/wsimport rather than /src/main/java.
This is a correct assumption.
The problem is that wsimport needs target folder created before execution and it fails. Can I create that dir first using any maven plugin. I can do it using ant but i prefer to keep it in POM.
I never noticed this problem (and would consider it as a bug, a plugin has to take care of such things).
The weird part is that WsImportMojo seems to do what is has to by calling File#mkdirs():
public void execute()
throws MojoExecutionException
{
// Need to build a URLClassloader since Maven removed it form the chain
ClassLoader parent = this.getClass().getClassLoader();
String originalSystemClasspath = this.initClassLoader( parent );
try
{
sourceDestDir.mkdirs();
getDestDir().mkdirs();
File[] wsdls = getWSDLFiles();
if(wsdls.length == 0 && (wsdlUrls == null || wsdlUrls.size() ==0)){
getLog().info( "No WSDLs are found to process, Specify atleast one of the following parameters: wsdlFiles, wsdlDirectory or wsdlUrls.");
return;
}
...
}
...
}
Could you show how you invoke the plugin and its configuration?
I am trying to figure out how to aggregate my maven dependencies in a multi-module project. For example, if I have:
root pom/project1
root pom/project2
and I run mvn dependency:copy-dependencies, I end up with the dependencies in:
root pom/project1/target/dependency
root pom/project2/target/dependency
What I really want is that if I run the mvn command in the root pom folder, all of the dependencies to be copied to root pom/dependency. Is there a maven property that gets me the output directory of the root pom? (similar to ${project.build.directory})? I realize that I can just copy all the dependency folders to the same place after the fact, but I was hoping for something a little cleaner.
You will have to configure the dependency plugin to copy depdendencies to a particular location. This can be done by the outputDirectory configuration property.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${outputDir}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
But if you trying to do this for distribution, I'd recommend you create an assembly using the maven assembly plugin
The documentation says:
The Assembly Plugin for Maven 2.0 is primarily intended to allow users to aggregate the
project output along with its dependencies, modules, site documentation, and other files
into a single distributable archive.
What I really want is that if I run the mvn command in the root pom folder, all of the dependencies to be copied to root pom/dependency. Is there a maven property that gets me the output directory of the root pom? (similar to ${project.build.directory})?
No, because modules shouldn't actually be aware of that.
I realize that I can just copy all the dependency folders to the same place after the fact, but I was hoping for something a little cleaner.
The Maven way would to use the Maven Assembly Plugin and a custom descriptor. But if you're not familiar with the Maven Assembly Plugin and its descriptor format, it won't be easy.
A less clean but easier approach would be to configure the Maven Dependency plugin to copy the dependencies into the parent project using a relative path. Something like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<configuration>
<outputDirectory>../root_pom/target/dependency</outputDirectory>
</configuration>
</plugin>
But as I said, this introduces tight coupling between modules and the root project which is not good at all (and I wouldn't go further by including the goal invocation as part of the build, modules should remain independent and you should be able to build one module without "checkouting" the parent).