-
1. Re: ShrinkWrap load libraries from POM for JavaArchive
kpiwko Feb 18, 2014 5:51 AM (in response to srinivas.akula)Hi Srinivas,
usually, you want to put resolved JARs into a WAR or EAR archive.
If you indeed want to include classes in another JAR into your JAR file, you'd need to merge JAR archives together. It's the same concept as uberjar or fatjar.
You can do that following way:
JavaArchive yours = ShrinkWrap.create(JavaArchive.class).addClass(Class.class); JavaArchive[] libs = Maven.resolver().loadPomFromFile("pom.xml").importRuntimeAndTestDependencies().as(JavaArchive.class); for(JavaArchive lib: libs) { yours = yours.merge(lib); }
I guess this answers your issue 1) and 2). I'm not sure what you exactly meant by 3), please provide a code how exactly are you adding jar files to archive.
Thanks,
Karel
-
2. Re: Re: ShrinkWrap load libraries from POM for JavaArchive
srinivas.akula Feb 18, 2014 7:14 AM (in response to kpiwko)Hi Karel,
Thank you for the reply.But, it did not worked for me. I am facing two problem with it.
Case 1:
JavaArchive test= ShrinkWrap.create(JavaArchive.class).addClass(Class.class);
JavaArchive[] libs = Maven.resolver().loadPomFromFile("pom.xml").importRuntimeAndTestDependencies().as(JavaArchive.class);
for(JavaArchive lib: libs) {
test= test.merge(lib);
}
If, I do not give any name to test archive, then I ran into java.lang.SecurityException. Please see case1.log file.
Case 2:
JavaArchive test= ShrinkWrap.create(JavaArchive.class, "test").addClass(Class.class);
JavaArchive[] libs = Maven.resolver().loadPomFromFile("pom.xml").importRuntimeAndTestDependencies().as(JavaArchive.class);
for(JavaArchive lib: libs) {
test= test.merge(lib);
}
If I give name to the archive, say "test", please see case2.log
And for 3)
File[] crLibs = Maven.resolver().loadPomFromFile("pom.xml").importRuntimeAndTestDependencies().asFile();
addPackages(crLibs, archive);
private static void addPackages(File[] libs, JavaArchive archive) throws IllegalArgumentException, ArchiveImportException {
for (File lib : libs) {
//for each lib, create JavaArchive.
JavaArchive jarArchive = ShrinkWrap.createFromZipFile(JavaArchive.class, lib);
//Read the archive for class files and extract the package information from it.
for (Map.Entry<ArchivePath, Node> entry : jarArchive.getContent().entrySet()) {
String path = entry.getValue().toString();
if (path.endsWith(".class")) {
int lastIdx = path.lastIndexOf("/");
path = path.replaceFirst("/", "").replaceAll("/", ".");
archive.addPackages(true, path.substring(0, lastIdx));
}
}
}
}
After deploying the final archive it gives, java.lang.IncompatibleClassChangeError
If you need further more info, please let me know.
Thank you.
-
case1.log.zip 1.1 KB
-
case2.log.zip 924 bytes
-
-
3. Re: Re: ShrinkWrap load libraries from POM for JavaArchive
kpiwko Feb 18, 2014 7:16 AM (in response to srinivas.akula)As for Case 1, I believe that the problem is that some of the dependencies your fetch are signed jar(s). You can either pass a Filter object while merging to reject all signature related bits from merged archive or you can call archive.delete() and remove them in the end.
As for Case 2, definitely don't forget to add ".jar" to archive name. Otherwise your application server might have problems loading it. The bug itself says that com.converis.rrr.test.RRRDataNGTest is not in the deployment. Can you verify that?
The best way is to allow export in arquillian.xml, that way you'll be able to browse what arquillian will deploy and you can also deploy manually to check - see export the deployment section here - http://arquillian.org/guides/getting_started_rinse_and_repeat/
Karel
-
4. Re: Re: ShrinkWrap load libraries from POM for JavaArchive
srinivas.akula Feb 18, 2014 8:13 AM (in response to kpiwko)Hi,
can you give me an example on
how to pass a Filter object while merging to reject all signature related bits from merged archive or you can call archive.delete() and remove them in the end? -
5. Re: Re: ShrinkWrap load libraries from POM for JavaArchive
kpiwko Feb 21, 2014 3:48 AM (in response to srinivas.akula)For deleting stuff: Just call archive.delete("/path/you/want/to/delete"), such as war.delete("/WEB-INF/classes/META-INF/persistence.xml");
For filtering stuff: archive.merge(jarYouWantToMerge, Filters.exclude("regex"), such as war.merge(jar2, Filters.exclude("(.*META-INF.*)|(/MANIFEST.MF)"). You can also construct your own filter programatically by implementing Filter interface.
-
6. Re: Re: Re: ShrinkWrap load libraries from POM for JavaArchive
srinivas.akula Feb 21, 2014 7:17 AM (in response to kpiwko)Hi,
I added some filter to exclude these files. But I ran into java.lang.IncompatibleClassChangeError.
here is my code snippetJavaArchive archive = ShrinkWrap.create(JavaArchive.class, "test.jar").addClass(Class.class);
JavaArchive[] libs = Maven.resolver().loadPomFromFile("pom.xml").importRuntimeAndTestDependencies().as(JavaArchive.class);
for (JavaArchive lib : libs) {
archive = archive.merge(lib, Filters.exclude("([^\\s]+(\\.(?i)(MF|SF|RSA|LIST))$)"));
}
archive.addAsManifestResource("persistence-template.xml", "persistence.xml")
.addAsResource("rsl-test.xml")
.addAsResource("log4j.properties")
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
return archive;
I am doing some thing wrong in this?
-
error.log.zip 1.4 KB
-
-
7. Re: ShrinkWrap load libraries from POM for JavaArchive
kpiwko Feb 24, 2014 6:55 AM (in response to srinivas.akula)Hey Srinivas,
I believe this has nothing to do with ShrinkWrap or Arquillian. I suggest to:
1/ Enable deployment export in arquillian.xml - see Export the Deployment in http://arquillian.org/guides/getting_started_rinse_and_repeat/
2/ Compare deployed archive with one that represents your application you get via your build tool.
I expect you deploy more artifacts than you need to and some of them (Eclipse JAXP) collides with classpath provided by GlassFish.
Karel
-
8. Re: ShrinkWrap load libraries from POM for JavaArchive
srinivas.akula Feb 25, 2014 7:51 AM (in response to kpiwko)Hi Karel,
Yes you are right. When I compare the two archives, there are lot of differences.
But I want to control them not to add to my archive.Here is my case:
Previously, I added packages manually to my final archive like this
.addPackages(true, "org.apache.log4j")
.addPackages(true, "org.apache.commons")
.addPackages(true, "org.slf4j")
and this makes sense, when I know the dependencies in my module. But, it is not possible to know all the packages from other maven modules, where my module is dependent on them.
For now, I am checking all the modules, where my module is dependent on and adding them in the above fashion.Later, I used Maven Dependency resolver from ShrinkWrap and I've given my pom.xml, so that it will add all the dependencies. But, it is adding them transitively. So, all other jars also added which I do not use them directly in my module. Finally my tests are not running.
So, is there any way to select only the dependencies which I need or to restrict the artifacts which I do not need?Thank you.
-
9. Re: ShrinkWrap load libraries from POM for JavaArchive
kpiwko Mar 3, 2014 6:54 AM (in response to srinivas.akula)Srinivas,
Sorry for the late reply. Definitely, this is possible. The best thing you can do is to follow official resolver documentation at resolver/README.asciidoc at master · shrinkwrap/resolver · GitHub .
I think that bunch of the artifacts are coming from <scope>test</scope> you include by calling importRuntimeAndTestDependencies(). I believe you want just importRuntimeDependencies(). See also section about strategies to get more details how to exclude even more stuff.
Karel
-
10. Re: ShrinkWrap load libraries from POM for JavaArchive
srinivas.akula Aug 13, 2014 10:55 AM (in response to kpiwko)Thank you. This is working now.