How add dependencies to generated sources - java

I developed my own maven plugin, which has a goal of generating sources. These sources have dependencies from my plugin code and are not compiling without it.
Is there any way to add plugin code dependencies to generated sources?
Or more precisely, is there any option beside adding plugin and dependencies separetly in pom?

Related

Export maven as jar file with all dependencies

I have my own java library created as maven project and has some dependencies included in pom.xml
I want to export project as jar and include it into others maven projects.
The problem is that I need to copy all dependencies from pom.xml of my library into maven projects where is imported my library to make it to work.
How to export my library to not be necessary to copy dependencies of my library.
That is easy to do; the central feature of Maven is that it manages the project dependencies for you.
You need to mvn install your project from the command line; that will install the jar and the pom files to your local repository.
You can then include your library as a Maven dependency in other Maven based projects; Maven will resolve the (transitive) dependencies for your project.
Normally you don't need to list all the dependencies in the project that imports your library. Maven should fetch them for you. What you need to do is to declare dependencies in your library.
Make sure you declare correct types of dependencies. Here is more info. In your case you need to make sure that dependencies you want to copy to the downstream projects are marked as 'compile'
There are tools that make 'Fat' jars by copying all dependencies inside. But they are mostly used to build the final project such as a deployable WAR file or a desktop app. Not in case of the libraries

Import Maven Project as Dependency into Gradle Project

I went through this link to import a gradle project as dependency into another gradle project. Is there a way to include a maven project as dependency into a gradle project?
If that Maven project is built somewhere else and deployed to a Maven repository, you can specify the artifact it produces as a simple compile dependency. If this Maven project is somehow a subproject of a Gradle multi-project build, I suppose you could hack it to work by simply ignoring the Maven POM file and perhaps adding a build.gradle to that project.
To use the solution described on the link that you provided - both projects must be gradle and included in gradle settings. Therefore you can use project closure to compile and depend on the project without building it explicitly.
I am not aware of any way to do this with maven project. I understand you use some maven plugins that you dont want to rewrite in gradle as simply can not find any equivalents etc. Often had that problem.
In this scenario I would suggest to build maven project and depend on a built jar in your gradle project.
Otherwise you could probably amend sourcesets in your gradle project to include maven classes. But I think it would be to complicated.
If I would be you I would turn it into gradle and try to replicate what you had using maven or just build the artifact and depend on it in dependencies closure.
Gradle is not that new anymore and there are many plugins that are superseding old good maven stuff.

How to use maven to generate code and package it without dependencies?

I have a project that generates some classes and resources that should then be packaged into a jar.
The class that does the generating has several dependencies. The generated classes have no dependencies.
My initial solution was to use the maven assembly plugin to only include the generated files. Unfortunately, the packaged pom includes all the dependencies required to do the generation. Consumers of this jar pull in a bunch of unnecessary dependencies.
I looked into the maven shade plugin. I compile once, run the generator class with mojo's exec plugin, the compile a final time. Then shade of course runs in the package phase. It creates a dependency-reduced-pom.xml without the excessive dependencies. So run mvn clean package and look in target/foo.jar. I expect the jar in the meta-inf folder to be the reduced dependency jar. But it's the full pom. I don't know how have the generated pom replace the one that is packaged.
Another poor solution I tried was using multiple profiles with their own dependency section. I run maven once with the generating profile, then again with the packaging profile. This works, but sucks because you have to run multiple maven commands. It's not jenkins friendly.
Am I going about this the wrong way? How should I go about arranging a project that generates some code and some text files that are then packaged in a maven friendly artifact?
1) One possibility is to make all your dependencies <optional>true</optional>.
The pom will still have the dependencies but the projects using your library won't try to download them.
2) Use plugin dependencies with the exec plugin.
That works well if the generator class is defined on another project.
Ideally you would call your generator using a plugin and avoid adding those dependencies to your project. That may not be worth doing depending on what your project is for and how reusable your generator has to be.
3) If you are using maven-shade-plugin the dependency-reduced-pom.xml is normally used. Check your .m2 repository to see the contents of the pom that is copied there when you do mvn install. It's likely that its contents will match the dependency-reduced-pom.xml

How to list missing dependencies of Maven project

I have a Maven project with 100's of dependencies. Some of these dependencies are not available.
When I run mvn install against the parent pom file (this parent pom references the other projects) then I receive errors that some Maven dependencies cannot be found. But I have to add those dependencies before I can discover what other dependencies may be missing.
Is there a Maven command which will list of the missing dependencies for a project ?
Update :
Another options is to clear out repository folder and attempt to rebuild with Eclipse/Maven plugin - missing dependencies for all projects appear to be listed in problems tab
Maven shows you all dependencies that are missing in the first tier. It's impossible to show also traversing dependencies because without poms included in those dependencies, Maven has no information about further dependencies. So only solution in that situation is to add all missing dependencies (absolutely with pom files) and then re-run maven (install or just mvn dependency:resolve)

What is the difference between Maven Dependencies and Referenced Libraries in Eclipse?

In my maven eclipse project I see Maven dependency and Referenced Libraries. In some cases they have the same set of jars referring to M2_REPO. And in some they are entirely different. Leaves me confused as to why there are 2 different jar references in the same project.
Maven dependencies are added in pom file to a project. When you build the project, maven dependencies that you have added in pom file will be downloaded from the M2 repository.
Reference libraries are added manually for projects in Eclipse IDE.
When you leave confusing for those jar files, just add all libraries as maven dependencies.
Classes in both Referenced Libraries and Maven Dependencies are visible in Eclipse but Maven build can see only dependencies from pom. If you try to build the project with maven it may fail because of this

Categories

Resources