Export Maven project with subproject to jar-file - java

I'm writing a lot of plugins for minecraft bukkit server's and I've grown tired of copy+pasting the same utility classes in my projects all over again. I decided to just put them all in a separate project and add them to my plugins via maven. I'm using IntelliJ Ultimate.
I want to have a maven project that contains all my utitily classes called e.g. UtilityAPI. Then I want to be able to create another project, which will be my bukkit plugin. In this second project I want to be able to use all the code from the first one. Also, I'd like it very much, that if I choose to build a plugin jar, maven automatically takes into account the most recent code from my API-Project.
I started working on this and started reading about maven modules and how you can use them to forge different projects together. I initially thought, that this was just what I needed, and tried to just add
<modules>
<module>UtilityAPI</module>
</modules>
However this results in my bukkit plugin project being considered a parent project, and refuses to build in a jar, but just in a (at least for me) rather useless .pom file. I'm not sure how to proceed. Do I have to create a "parent" project for my bukkit plugin project which contains the api and the plugin project as modules? And if yes, how do I generate a .jar, and not a .pom?
The dream solution would be to have the UtilityAPI project, and being able to include it in any new plugins that I might write in the future. I'd also be a fan of having a simple way to create a jar with the newest sources of my plugin in it. Is this possible, and if yes, how?

In your Maven multi-module project your plugin would have to be another module (and not the parent, which has packaging type pom). This module would then have a dependency on the API module (dependencies between modules are possible).
However, multi-module projects are usually intended for projects which are tightly coupled. That does not appear to be the case for your scenario. It sounds like the plugins have (potentially) nothing in common except for the dependency on the API project. Maybe it would be better to have the API project as separate standalone Maven project and then deploy snapshot versions of it (or install them only to your local Maven repository) and use these in your plugin projects.

Related

Recreating GitHub project without dependencies (Java)

I would like to use a GitHub repo inside a Script I'm writing. The Script will run inside an application which requires that the Script has minimal dependencies. By this I mean it can have a dependency on a standalone .jar or library, but not on one that has further dependencies. This is a security measure. My Script needs to have a dependency on a GitHub project, but that project also has its own dependencies. Is there any way to compress that GitHub project and its chain of dependencies into one standalone library or .jar?
I'm using IntelliJ (most recent version) if that helps. The GitHub project I need to use can be one of the following:
https://github.com/RuedigerMoeller/fast-serialization
https://github.com/EsotericSoftware/kryo
I need it to serialize and deserialize large object structures very quickly and frequently, otherwise my program doesn't operate on current data.
EDIT: So I did solve this issue, the solution was to use the Maven Shade plugin to compile an uber or fat .jar of the Maven project. This allowed me to bypass the security measure.
Having a dependency on a GitHub repo is having a source dependency (which might declare itself binaries dependencies in its own repo).
You would need to fork that repo, and transform its maven project in order to generate a fat jar (with for instance the Shade plugin).
And you would need to publish that new artifact to an artifact repository (like your own Nexus) in order for your project to depend on it.

IntelliJ Idea with Gradle: Best Practices

I am developing a Java application using IntelliJ Idea 14.1.4.
If it would have been solely Java application, I would have known exactly how to structure the project in Idea:
A single Java project, containing several modules: One for each part of the application (JAR).There will be at most 4-5 JARs.
The dependencies between the modules are also known: Protocol does not depend on anything, everything else depends on Infrastructure, and so on.
Next, I would like to use Gradle scripting for managing the project. So my question is what is the best practice to structure the code in Idea?
Should I create a single Gradle Project and a Gradle module for each of the JARs?
Should I create a single Java project (or maybe empty project) and Gradle modules for each of the JARs?
Should I create a single Gradle project and each of the modules will be a Gradle's sub-project? Maybe it will be better to have an empty project and several Gradle modules because not all of the JARs are closely coupled?
Since I have never used Gradle before, I would appreciate any guidance for the best practices when combining both Gradle and Idea.
Thanks,
Guy
As long as possible, I would keep the code in one source repository. On the root, I would have an "empty" project not outputting anything. All your jar projects would be sub-projects (in Gradle terms). You include them via the settings.xml file located in the root project.
Each sub-project has its own build.gradle file. In those files, you can easily define the dependencies between your sub-projects, e.g.:
dependencies {
compile project(':subProject3')
}
For convenience, I often create a special export task to put all artefacts in one export/ folder on the root level so that you don't need to go through all those sub-folders to get your stuff.
task export(type: Copy) {
from project(':subProject1').jar
from project(':subProject2').jar
from project(':subProject3').war
into 'export/'
}
IntelliJ Ultimate 14 works fine with this approach. You can simply hit Make to compile everything. You might also want to configure your project settings to run gradle jar or gradle export during a make if you prefer.

Maven/Eclipse plugin: easiest way to have new Maven project have dependency on legacy non-Maven project?

I created a new Maven project in Eclipse. This was working fine until I needed to add a dependency to another Eclipse project, a legacy utility project, which does not have a pom.xml, and does not have the directory structure of a typical Maven project. (It has the directory structure of a typical eclipse Java project). This other project is in the same Eclipse workspace as the Maven project.
In looking at other posts on this, it seems that usually the solution is to build the jar for the other project and install it in Maven. However I am actively modifying code in the utility project while writing code in the Maven project, so I can't just install a jar once to satisfy the dependency.
What is the easiest way to handle this so that I can code simultaneously in both projects, and also get maven to build cleanly? (Of course Eclipse can build just fine with just a project dependency.)
UPDATE
Using the Build Helper plugin to add the utility projects source folder to my pom was a viable path to the solution, but then I needed to update all the dependencies of the utility project into my new Mavne project, which started to make the whole process too time consuming (and also not really the chain of dependencies I wanted). I think that if I would have added all the dependencies, then Build Helper suggestion would have worked.
For now, I built the utility project jar and installed it into maven. Which turned out to be the the quickest solution. I will try to Mavenize the utility project, without modifying its structure (as suggested by FrVaBe below), and see if I can link the poms afterward.
I am going to keep this question open until I have a full solution which can be reported back, since I assume this is a problem others will have (trying to integrate legacy projects with new maven projects).
For the development time you can add the dependency as a System Dependency. It will be resolved by the file path (which can be the path to your utility.jar file under development) in this case.
It is added as describe in the link above, e.g.:
<dependencies>
<dependency>
<groupId>my-utility</groupId>
<artifactId>my-utility</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${somewhere}/lib/my-utility.jar</systemPath>
</dependency>
</dependencies>
The maven handling of System dependencies is sometimes special. E.g. they will not be included in war-packages! Therefore, when you are finished I would strongly recommend to install your utility library to the maven respository or to deploy it to a repository manager (Nexus/Artifactory).
You can add utility project's src folder to your working project in eclipse. For your development purpose.
right click on Working project
go to properties and choose java build path
go to source tab
Add your utility project src folder to that.
Later you can install your jar as maven dependency.

Refactor classes into a separate project

I intend to extract several classes and packages from one Java project and place them into another (which will have a distributable jar). That much isn't too difficult, but of course with such a large refactoring there are consequences. Namely there are many classes in the original project that are subclasses of the classes I want to extract. What's the best method for approaching this sort of refactoring?
You can create separate projects and the main project will have dependencies for all these projects. So in your IDE you can navigate through source code easily.
When building your application, each dependency could be built into a jar and the main application will be bundled with all the dependents jars in its classpath.
Let take as example a web app using plugins, and plugins using common classes, utilities and so on stored in a project named common-plugins.
project/webapp: having dependency on plugin1, plugin2 and common-plugin
project/plugin1: having dependency on common-plugins
project/plugin2: having dependency on common-plugins
project/common-plugins: having no dependencies
When building your project, you could build the plugins and the common-plugins into jars, bundled with your web app
project/webapp.war/WEB-INF/lib/plugin1.jar
project/webapp.war/WEB-INF/lib/plugin2.jar
project/webapp.war/WEB-INF/lib/common-plugins.jar
This way in your IDE, I will take eclipse for instance, you will have a workspace having 4 projects with dependencies as described above. At build using maven, ant, ivy, or what you want, you will build the 3 projects that the webapp project depends on, then bundle the whole stuff.
So basically this is what I did:
Create a new project
Copy over the appropriate classes from the old project to a new package in the new project, reconfigure until everything builds
Test that project separately and build it in to a jar
add jar as a dependency
Delete the classes from the original project
Manually change all the imports from the old packages to the new packages
What I was really looking for was some way to automate or streamline step 6 to make sure I didn't break anything, but I'm not sure it exists beyond mass find/replace.

Build play! project with maven

I am using play 1.2.4 to create a web app. It works together with some other applications which are maven projects: they are compiled and resolved (dependencies) with maven.
Now I need to modify my play app so it can also be compiled and started with maven.
Therefore I download maven (3.0.4), installed the maven modul and did everything according to https://github.com/wangyizhuo/play-maven and http://www.playframework.org/modules/maven-head/home
I manage to automatically create a pom file in my project. The problem is that the jars from the play framework are not copied into the lib-folder and when I try to execute mvn package they are missing.
Is there a way to get the maven module to work properly? If not, is there another possibility to compile and start the project from maven (i.e. intermediate ant-file that can be executed by maven?)
Many thanks in advance!
This explains it pretty well. I havnt looked at the module you are refering too, but this shows you how you can create tasks and inculde directories in your build.
Do you need to use maven to build/run it? If not it looks like there is a community contributed extension that allows you to use maven from within play.

Categories