How to add one package with all classes and subpackage from provided dependency into the Jar.
I have got dependency like :
<dependency>
<groupId>com.a</groupId>
<artifactId>dependencyA</artifactId>
<scope>provided</scope>
</dependency>
What I want to achieve is to add all classes and subpackage into jar file. For example Is there any plugin that allow me to specify which package should be added something like:
<include>com.a.package.*</include>
As a result, I expect that all classes under package com.a.package.* from dependencyA would be added into jar.
You can't as you're defining a dependency, so your application needs that JAR fully, but you only import some packages, those what you need, so the JVM will only load those ones.
Related
I try to import a dependency in POM in the following way:
<dependency>
<groupId>org.test.maven-presets</groupId>
<artifactId>caffe</artifactId>
<version>preset-1.3.3-SNAPSHOT</version>
<classifier>pojos</classifier>
</dependency>
There are 4 jar files: 1) caffe-preset-1.3.3-SNAPSHOT.jar 2) caffe-preset-1.3.3-SNAPSHOT-javadoc.jar 3) caffe-preset-1.3.3-SNAPSHOT-sources.jar and 4) caffe-preset-1.3.3-SNAPSHOT-pojos.jar.
My requirement is only I have to use caffe-preset-1.3.3-SNAPSHOT-pojos.jar. But even though I use the classifier tag, I am still fetching the classes that are under caffe-preset-1.3.3-SNAPSHOT.jar. How can I only use the classes that are under the tag?
Like I have only the POJOs under 4th Jar which is used as a dependency in another project.
I can add these POJO classes in another project and create a Jar out of it separately and can use in other projects, but our technical team management is not agreeing to create New Project, as it requires different approvals and has to Justify to each of them clearly.
Could anyone please help me to get through the requirement? Thanks.
I have added two projects as modules in empty intellij project.
Then I added in pom of module B following dependency to first project(module A):
<dependency>
<groupId>Tests</groupId>
<artifactId>Group</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
This allows me to import classes from module A into module B.
But I can't see any method from that module (it looks like classes are empty or they have only private fields/methods).
What am I missing? What should I do to see all public methods/fields from A module?
Thanks
Kamil
If you are adding one of them as a dependency, you can avoid to join them as modules. For local purposes, you can build(mvn clean package) one of them and add that as a dependency to another one. You can check relevant .class file to see the access levels of the class members.
For multi-module projects, please, see: https://www.jetbrains.com/help/idea/creating-and-managing-modules.html
I have a multi module maven project, and in the dao module, I added the JSON-IO dependency. When I try to deserialize my object, it gives me:
Exception in thread "main" com.cedarsoftware.util.io.JsonIoException: Class listed in #type [hu.kleatech.projekt.model.Employee] is not found
The class name is correct, the Employee is public, and the dao has the module as dependency. What could have gone wrong?
Edit: Since this is an old question and have been answered long ago, I'm deleting the github repository that I made specifically for this question. The solution to the problem is in the accepted answer, the exact code is not relevant.
Please try adding an empty constructor to Employee class.
Edit: Actually, while adding an empty constructor solves the problem, it is not necessarily required. Json-IO "will make a valiant effort to instantiate passed in Class, including calling all of its constructors until successful. The order they tried are public with the fewest arguments first to private with the most arguments."
(copied from MetaUtils.java javadoc)
Also, when calling a multi-argument constructor, the library fills the arguments with nulls and defaults for primitives. Then any exceptions thrown during the constructor call is ignored. In your case, a NullPointerException was thrown, because the constructor is not null-safe. So either modify the constructor so that it can handle nulls, or add an empty constructor.
Maven dependency configuration is hierarchical from <parent> element not from <modules> element.
It means that in the project's pom.xml file where you have dependency on "JSON-IO dependency" you do not have dependency on your dao project or where that class is.
<modules> stands only to define what projects to build. Order of modules definition does not matter, since Maven detects order by required dependencies
So, you can define dependency in <parent> pom.xml either in
<dependencies> element. then all children will have it.
or in <dependencyManagement> - then children who need it can include it in their <dependencies> without common configurations like version, scope etc...
look at quite similar answer here:
How to minimize maven pom.xml
As per your project and modules Pom your main Pom should have modules in following order ....
<modules>
<module>core</module>
<module>controller</module>
<module>service</module>
<module>dao</module>
</modules>
service depends on core so core should be build before service
dao depends on service and core both so dao should be after core and service.
Employee class is available in core and it should be available in core jar.
You should add depencyManagent in main Pom and then add all the module as dependencies in dependencyManagement so whoever adds your main Pom as dependency will be able to access all your jars.
Once you change order build your project again and then update your maven project.
If this code is being used in another project then make sure that you have uploaded jars to repository (mvn deploy) so whoever uses it can download it when they are building their project.
One way to verify whether this jar is downloaded in the main project where it is used or not is check in project explorer there would be a Maven Dependencies section where you can see all dependency jars and check if core is present or not.
I am not sure what controller module is doing in main Pom as I couldn’t find a module by that name in your project so you should either remove it or add a module (folder) for it.
There are two bundles (A and B) which exports package a.b.c and B is a third party bundle. A is used as a dependency by other modules which has imports for a.b.c. These modules should get the package from bundle A and not B. Without excluding a.b.c package from B using the <Export-Package> it is possible to provide a workaround to bundle A to restrict the modules to only refer the package from itself?
In the module that uses A that already has a,b,c from other places, place an exclude inside the A dependency tag for that pom.
So, I'm confusing myself with the letters but if project BOB uses dependency A but you don't want BOB to pull a,b and c from A because it already has that from elsewhere then, in the pom.xml for BOB, within the A tag you'd use and exclusion.
<exclusions>
<exclusion>
<groupId>sample.a</groupId> <!-- Exclude Project-a from Project-A -->
<artifactId>Project-a</artifactId>
</exclusion>
</exclusions>
Check out the full example at 'Exclude Dependencies'
After adding this dependency in my pom.xml:
<dependency>
<groupId>com.eaio.uuid</groupId>
<artifactId>uuid</artifactId>
<version>3.2</version>
</dependency>
I get an error by jetty on the module load event:
no source code available for com.eaio.uuid; did you forget to inherit the module? unable to find com.client.myproject..`
What am I missing?
If you're using any of the classes in that artifact in your GWT compiled code, then the source code needs to be available, either packaged in the jar or as a source jar (remember that's another dependency).
You'll have to look for a .gwt.xml file in the jar, as this will be the name you need to inherit in your own GWT descriptor, eg. if the file is called com/eaio/UUID.gwt.xml you should
...
<inherits name="com.eaio.UUID" />
...
If one isn't available, just create your own with a simple <source path="..." /> and stick in the right package in your own project (still provided the source is actually available!)
Cheers,