Java Maven multi modules + proguard obfuscation - java

I am in the process of transferring a java netbeans project that has proguard obfuscation, from ant to maven multi module. I am a newbie in maven, and after reading and trying many solutions & architectures for a few days now, I am still quite lost.
From what I have seen, there are to ways to structure my project:
option 1: option 2:
module top module top
module A module A
module B module B
module C module C
module assembly
The modules have inner dependencies between them.
'module top' is a module that has very little code, and it acts as a wrapper.
In option 1, the pom.xml for packaging the project is in 'module top'.
In option 2, 'module assembly' has no logic, just the pom.xml for packaging the project.
My goal is to package the project in one fat jar where the inner modules will be obfuscated using proguard plugin.
What I have tried so far:
I managed to obfuscate one module, but because the modules are dependent on each other, I need to keep the entire interface structure, otherwise the modules don't fit when wrapped together.
I have created a fat jar file of the entire project and used proguard to obfuscate it. Since it is a big file, around 20M, the process took a lot of time, around 15 minutes. The output file was small, about 4M, and it didn't work.
Right now I am trying to assemble all my code during the build to a distribution jar, and separate it from the library jars. my aim is to obfuscate only my modules, and add the libraries later (this is what I have seen is working for me in the ant project that I currently have).
So far all my attempts have not yielded a process that works. I can't attach the pom files because I am keep changing them when trying new stuff.
So to sum this up, these are the options as I see them:
Build & obfuscate each module separately, combine them in the top module while dealing with keeping the interfaces.
Assemble & obfuscate all my modules together, separate from the libraries.
Build a huge fat file, and obfuscate it as a whole.
Do the packaging inside 'module top' or using a dedicated 'module assembly'
What do you recommend? Any help would be appreciated
EDIT
I got a small breakthrough. using option2 and proguard GUI, I finally managed to create a working obfuscated jar. For first step I run maven-dependency-plugin with copy-dependencies to a lib folder.
Then in the proguard GUI I 'injar' all my modules, and 'libraryjar' all the rest, and created dist-obsfucated.jar
My current ModuleAssemble structure is like this:
project
target
dist-obsfucated.jar
lib
moduleA.jar
moduleB.jar
moduleC.jar
other_file1.jar
other_file2.jar
How do I assemble all the jars to an executable jar? I need tell the assembly plugin to ignore the unobsfucated modules A through C in the lib folder
EDIT #2
I managed to solve it using 'maven-antrun-plugin'

Related

How to get folders, resources and pom file from a maven project to another maven project?

I am trying my luck in eclipse to achieve something that I am able to successfully achieve using Visual Studio(.net project). I have a core-framework maven project with additional files and folders apart from the normal folders that maven provides. I exported it as jar file and added it as an external library to another maven project. Is there a way that when I add it as a library to the second project, the second project gets all the folders, files from the first maven project overwriting the pom file in the second maven project too? Inshort I want to make sure whoever takes the framework jar as reference follows the same folder structure as framework with required files such as config file, pom.xml file to avoid errors on missing path/files. I read about dependency management but even for that I have to define all the dependencies in child pom file which I want to avoid. Any help would be appreciated, I didn't find much info around the query.
Copying JAR files to lib folders is old-school. Such you also set aside Maven's sophisticated dependency (and transitive dependency) resolution. The clean Maven way is to put your core-framework JAR to a Maven repo (remote or local at the users machine) and let the users of it declare it as dependency in their projects.
What do you exactly mean by "additional files and folders apart from the normal folders that maven provides"? What is there more than code and resources?
To make your users' life easier concerning dependencies you could a) use a BOM (Bill Of Material) dependency or b) use inheritance (i.e. <parent> ← child relationship).
Adding a dependency will not change your project in any way. You cannot add folders from a dependency to the project.
You can write a Maven archetype that is a kind of project template. Then people can use it to create new projects where the files are at the right places.

Eclipse: how to properly integrate a Maven/Ant project into a larger one?

I have a Java project that depends on a 3-rd party component. This component is available both as a jar and as a Maven/Ant project. One option for me is to simply add the jar as a library. However, I prefer to add the source code into my project since I may need to slightly modify their source code to better suit my needs.
What's the right way to do it in Eclipse?
My main project has a simple structure: src/ and lib/. The external component also has a standard structure: src/, test/, build/, target/, pom.xml, build.xml. So do I need to copy piece by piece (like contents of one src/ into the other src/), in which case what goes where? Or do I somehow copy it all at once? Or smth else?
The best way would be if you use maven on your projet for dependency management. This way, if you have the other projects open in eclipse, your project will resolve them as local projects, but if you don't, maven will try to fetch the jars from the configured nexus repository.
This way, you avoid having to manually configure your Eclipse projects. Maven will be able to configure your project anywhere you want to build it, not having to manually configure dependency resolution.
Import both the projects into eclipse. Add the reference of 3pp jar project to your project as a reference by clicking on Add on build path option. While delivering it as output there will be a dependency to the 3pp jar project. So either deliver it as separate jar and add it to classpath while executing your project else you have to copy the entire source files into your project and deliver it test complete jar.
Making a jar will be handled by eclipse itself.

Does using Checkstyle Plugin for use within large multimodule projects require a separate project?

I went through https://maven.apache.org/plugins/maven-checkstyle-plugin/examples/multi-module-config.html to configure checkstyle plugin for multi-module projects. This requires creating a separate project just to house the resources which could be hardly 4-5 files(at least in my case). Can't I just keep those files inside a resources folder in parent-project? Will those files be available on the classpath of every inheriting child?
In simple terms, I don't want to create a separate project just to hold a few files (if this doesn't make any difference)
No you do not need to create a separate project to house the checkstyle resources. Those resources house the checkstyle rules that need to be available to the plugin when it runs. Putting the checkstyle rules into its own project, and then adding them as a dependency is just the most convenient way of accomplishing this. Of course you can have every project have its own copy of the checkstyle runs.
If you put it in the parent project then that could work. If the project is packaged as a jar.

Java Maven project minify the build

I am working on a multi module Maven project. We have created a project which builds an uber jar. The unpacked jar is about 60mb which is a problem for our client.
Are there any tools we can use to remove unused .class files within the dependencies when the build is completed?
Another option would be if we could analyse what .class files are loaded when running and produce a list of these and thus remove others from the build manually.
cheers,
I'm not sure if it could remove classes as such, but proguard has some awesome results in terms of minifying class sizes. You can see from the link it reduces the Ant module by 90%

package all external classes in my jar, with Eclipse

I am working on a Hadoop project in Eclipse that depends on another one of my projects; I've included the other project in my build path, but when I export the dependent project, it only contains the classes from that same project.
Ordinarily, this would not be a problem, as I could just link the other project with the -cp flag, but Hadoop requires you to pass the jar as an argument, meaning that all of my dependencies must be inside that jar.
Is there a way, in Eclipse, to automatically build and include classes from projects that you depend on?
Thanks.
You coud use Ant to automatically build, test and export. It needs some time learning it, but its worth.
There are possible tasks (fileset, zipgroupfileset, copy) to include files, jars (unzipped) or anything into the final jar. By this way you definitly know whats inside your distribution jar and you don't need an eclipe installation running.
I suggest you take a look at maven as a build tool. You define the dependencies and build steps for each of your projects in files called pom files. The maven plugins for Eclipse (the m2e plugins) can take the configuration in the pom file and setup your Eclipse build paths and project description so that you can access the classes in your other project in Eclipse. Maven can also create a jar for you that has the classes from both projects (a "jar-with-dependencies").
In maven terms, your two projects are called "artifacts" with one having a dependency on the other.
The one downside to maven (and the cause for many negative comments about maven) is an initially steep learning curve that can be frustrating. What you're trying to do, however, is very straightforward and I expect you can find a number of examples showing you exactly what you want to do.
The first step, and that's what my answer is about, is to take a look at maven. It may seem overly complex, but it can scale to handle just about any build configuration you need as your hadoop apps get more and more complex.
You can export a project as a Runnable jar, which can be useful if you want a single jar, with dependencies included.
Select the Project. File > Export. Select the Java section. Select Runnable JAR file.
See related answer:
Eclipse: How to build an executable jar with external jar?

Categories

Resources