Need help packaging a webservice client jar in Java - java

We have a series of web service APIs that we have also written clients for so that we can easily re-use them in other Java applications. I now have need to be able to use the clients in an environment outside of a webserver. I used the maven-assembly-plugin to create a zip containing all of the client's dependencies and it worked fine except for the dependencies that would be provided by wildfly. I added the appropriate resteasy jar files to a command line app and everything works. Now I would like to be able to package the client 2 ways; with the resteasy jars and without. The apache shade plugin looks like a good option to do this but I can't see how to include an extra dependency (resteasy) that is not referenced in the project. Has anyone done this?
Edit:
I have tried both the maven-assembly-plugin and maven-shade. Shade looked promising until I attempted to deploy the jar. I got security warnings assumedly because the dependency jars that it repackaged were signed and so repackaging them caused errors. In any case it looks like using profiles is part of the key. I can set up a separate profile in the pom that will include the resteasy dependencies and so include them only when desired.

For others looking at this question, I haven't completely solved this but adding a profile is part of the key. If you add a profile to your pom, you can add the resteasy dependencies only under that profile. If you add the maven-assembly-plugin and the resteasy dependencies under that profile, you can build a jar containing all of the dependencies without affecting you regular release.

Related

How to include dependency as a .jar in resources with gradle?

I'm trying to share a dependency between multiple jars. My solution to this would be to include the dependency as a .jar in each one and then load the most up to date one at runtime (in order to not have multiple identical shaded versions, and to include resources from that dependency).
Essentially, I'm trying to make the compiled jar include dependency.jar as a resource - how can I achieve this with gradle? Or is there a better way of accomplishing this? I don't want to pull the latest version of the dependency .jar from a remote server as this has to work offline.
The easiest way of adding a dependency to your project without Gradle accessing a remote repository is to simply add the file as a dependency directly:
dependencies{
implementation(files("/path/to/myJar.jar"))
}
If you only want to stay offline you can run Gradle with the "offline" command line switch when you are offline. See the documentation.
To share across multiple modules you can declare the same file dependency in each module's build file.
Any attempt to compare versions between modules and/or load a JAR at runtime sounds excessively difficult to me.

Required jars for RestEasy Client

I need to provide a java REST client, which should contain all required jars in one bundle. I chose RestEasy as REST framwork, since the server application is done on a JBoss.
Nearly all examples I found so far use either an application container environment, where those libs are provided and thus only the Java EE API is needed during compile or are build with Maven and thus dependencies are resolved automatically, which maybe be a good idea and the current standard way to do it, but for project-related reasons I need the jars in a lib folder and be able to include during the build and wihtin an executable jar.
So my question is, which jars a necessary to build a simple client which can do something like that:
ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target(myURL).queryParam("param",
"value");
Builder builder = target.request(MediaType.APPLICATION_JSON).header("user", "abc");
Invocation invocation = builder.buildGet();
MyResponseObject response = invocation.invoke(MyResponseObject .class);
The easiest way is to use Maven. The reason I say this, is that the main artifact you want is the resteasy-client artifact, but this artifact has dependencies on other artifacts. If I create a new Maven project, add only this dependency
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>3.0.9.Final</version>
</dependency>
The project will pull in all this artifacts
But if you are not using Maven, You can download the entire resteasy package here. It comes with a lot more than what you'll need, but it will have all the jars you see in the image above, along with some other goodies like user guides, examples and such. Base on the image above, just get the jars you need. Make sure you download the final-all version. When you unzip it, all the jars should be in the lib dir.
Another thing I might mention is that in order to unmarshal JSON representation into your Java classes, you might also need resteasy-jackson2-provider. Doing the same as above, you will see these pulled in artifacts
Again, these are also include in the download. This will work in most cases, if you are using JAXB annotations (which could return XML or JSON), because of the pulled in artifact jackson-module-jaxb-annotations, but that artifact doesn't support all JAXB annotations, so you might need to pull in the resteasy-jaxb-provider, if need be. Again like I said, just the jackson2-provider may be enough. But in the case you do need the jaxb-prodiver, here's what it looks like
Again, included in the download
If you use maven in your project, you can type dependency:tree to see hierarchy of your dependencies. Libraries used by RestEasy will be listed in tree.

Indirectly referenced from required .class files

I'm getting below error in STS:
The type org.springframework.core.env.EnvironmentCapable cannot be resolved. It is indirectly referenced from required .class files
This sounds like a transitive dependency issue. What this means is that your code relies on a jar or library to do something - evidently, you depend on Spring framework code. Well, all that Spring code also depends on libraries and jars.
Most likely, you need to add the corerctly versioned org.springframework.core jar to your classpath so that the EnvironmentCapable class can be found when your IDE attempts to build your project.
This might also be a jar collision issue as well, although that sounds less likely. When an application experiences jar collision (also known as "dll hell"), the compiler is finding multiple jars and classes with the same fully-qualified name. For example, let's say you added Spring to your classpath, along with the entire Tomcat server library. Well, those two jars may contain the same exact named classes, maybe the same version, maybe different versions. But either way, when the compiler looks for that EnvironmentCapable class, it finds two (in this contrived example) - one in the Spring jar and one in the Tomcat jar. Well, it doesn't know which one to choose, and so it throws a ClassDefNotFoundException, which would/could manifest itself as the error you experienced.
I faced same error while i work with spring security on spring-security-config.i jsut deleted that jar in maven repo and gave maven->update Project in eclipse.
it is resolved.Please try it once.
From command line, run "mvn clean install", you'll see project failed and you'll see artifacts in the logs that cause such a problem.
After that, remove artifacts from .m2/repository, then maven update from eclipse.
To avoid jar collision, make sure you declare your dependency versions under the properties tag in the aggregate pom.xml, and use the property name as a placeholder throughout the project. For example 4.2.5.RELEASE in the parent pom, and then in the child modules just use ${spring.version} instead of 4.2.5.RELEASE. This way you can avoid having two different versions of the same library on the classpath.
Also it is recommended to be consistent with the version of spring dependencies. Use the same version for spring-core, spring-web etc.
If you are using maven, then you can use the maven enforcer plugin to ensure dependency convergence, and avoid further issues with transitive dependencies.

Maven and AppServer dependency management best practice

I am developing a web-app and use maven for dependency management (duh). Some of the needed jars are already available in the server lib folder, but do not match the "maven naming scheme", ie missing the version suffix.
I would like to use them for development and deployment, but..
1. i cant point maven to them because maven seem to need a version suffix. I cant omit it in the pom.
2. If i define the dependency outside maven then maven is obviously unable to build.
3. Renaming the files inside the server distribution sounds like a kludge.
What would Brian Boitano do? I mean, there sure is an elegant solution that im not aware of, or at least a good argument for one of the three solutions above.
Thank you
PS. i am using jboss 5.1 and maven 2.2.1 atm, but its subject to change
You can provide those jars as a dependency with a system scope if you want explicitly to identify where they live. For more info have a look here
IF those are not proprietary libs you are using, I'd recommend you use official versions of those from maven repository.
If they are proprietary you can manually install jar to your local repository using maven(you can use your version, suffixes, group names, artifactid etc) and then use them in your pom.

JIBX MAVEN Problem

I am trying to use JIBX maven plugin in my Spring Web Service project,
My project has diferent layers (API-DAO-SERVICE vsvs..)
these layers are individual projects and has maven dependency each other.
In my WEB project's POM, I add other project as dependencies, and my mapping
classes are all in API project.
Even in my web project's POM includes dependencies of API project I could
not use that class'es in JIBX
BUT IF I put that class'es in WEB project's src/main/java directory there is
NO PROBLEM,
How can I use mapping classes that are other maven dependent projects.
Any help is really appreciated;
Thanks.
Generated sources are supposed to be compiled and packaged in the artifact at the end. The classes should thus be visible from your webapp.
Did you setup the maven-jixb-plugin as shown in Generate Java Sources from Schemas? If not, then maybe update your setup first and try again.
And if you are still facing problems, please show the relevant parts of your POM(s).

Categories

Resources