Put Jar file into a folder and package as zip - java

Oracle Identity Manager application needs plugins to be deployed as I explained below:
There must be a zip file contains 2 folders and plugin.xml
folder structure is looks like this:
lib folder must contain project itself packaged as jar, META-INF folder must contain pluginPostProcessEventhandler.xml
This folders and file structure needs to be zipped as pluginPostProcessEventhandler.zip to be deployed. Using Jenkins. is there any way creating this folder and file structure as output folder by using pom.xml?
Thanks

With the Maven assembly plugin, it should be possible to create a zip in the way you describe.

Related

Generating a runnable JAR file

I'm trying to generate a runnable JAR file using Eclipse. In my bin folder, I have three folders all on the same level:
com
Properties
MiscFiles
In Eclipse, I have Properties and MiscFiles as Source Folders. When I go to export the JAR file as a runnable JAR, it includes the com folder but doesn't include the Properties and MiscFiles folder. I have to use an external program to manually add in those two folders. Is there a way to include these folders when exporting the JAR file?
Check your build.properties file.
And make sure that all the folders are added in it.
During jar generation it only considers the folders and files added into properties file.

JAR doesn't include root files

I have simple Java Application and trying to create JAR to distribute using eclipse.
But when I look inside JAR it doesn't contain the test.txt file. I created JAR as Export>Runnable JAR File
You need to put that file into one of your source folders (such as src). Only those get copied into the jar file (by default).
See the following image:
Choose the file which you want to add in jar file.

How to convert AAR to JAR

Situation:
I intend to use a Java library and I only have an AAR file from a Maven repository but I need the JAR file.
Background story:
I tried to compile a library, but its Gradle structure was unstable. So I asked for a compiled jar file, and its developer handed me an aar file from some Maven repository (the developer couldn't compile his own project).
The Gradle configuration was a mess, it depended on multiple libraries and Gradle has thrown some exceptions.
I tried to use it in my IDEA IDE, but it couldn't see it. A library project should be able to be compiled into a jar file, right?
Question:
What should I do to convert that AAR file to a JAR file?
The AAR file consists of a JAR file and some resource files (it is basically a standard zip file with a custom file extension). Here are the steps to convert:
Extract the AAR file using standard zip extract (rename it to *.zip to make it easier)
Find the classes.jar file in the extracted files
Rename it as you like and use that jar file in your project
.aar is a standard zip archive, the same one used in .jar. Just change the extension and, assuming it's not corrupt or anything, it should be fine.
If you needed to, you could extract it to your filesystem and then repackage it as a jar.
1) Rename it to .jar
2) Extract: jar xf filename.jar
3) Repackage: jar cf output.jar input-file(s)
As many other people have pointed out, just extracting the .jar from the .aar file doesn't quite cut it as resources may be missing.
Here are the steps that worked for me (context of Android, your mileage may vary if you have other purposes):
Rename the .aar file to .zip and extract.
The extracted folder is an ADT project that you can import in Eclipse with some minor modifications (see below)!
In the extracted folder rename the contained file classes.jar to whatever you like (in this example myProjectLib.jar) and move it to the lib folder within the extracted folder.
Now in order for Eclipse to accept it you need to put two files into the extracted folder root:
.project
.classpath
To do that, create a new Android dummy project in Eclipse and copy over the files, or copy over from an existing Android project.
Open the .project file and look for the XML name tag and replace the contents of it with myProjectLib (or whatever you called your jar file above) and save.
Now in Eclipse you can File -> New -> Project -> Android Project from existing source.. and point to the extracted folder content.
After import right click on the newly created project, select Properties -> Android, and check Is Library.
In your main project that you want to use the library for, also go to Properties -> Android and add the newly added myProjectLib to the list of dependencies.
For those, who want to do it automatically, I have wrote a little two-lines bash script which does next two things:
Looks for all *.aar files and extracts classes.jar from them
Renames extracted classes.jar to be like the aar but with a new extension
find . -name '*.aar' -exec sh -c 'unzip -d `dirname {}` {} classes.jar' \;
find . -name '*.aar' -exec sh -c 'mv `dirname {}`/classes.jar `echo {} | sed s/aar/jar/g`' \;
That's it!
Android Studio (version: 1.3.2) allows you to seamlessly access the .jar inside a .aar.
Bonus: it automatically decompiles the classes!
Simply follow these steps:
File > New > New Module > Import .JAR/.AAR Package to import you .aar as a module
Add the newly created module as a dependency to your main project (not sure if needed)
Right click on "classes.jar" as shown in the capture below, and click "Show in explorer". Here is your .jar.
Resource based .aar-projects
Finding the classes.jar file inside the .aar file is pretty trivial. However, that approach does not work, if the .aar-project defined some resources (example: R.layout.xyz)
Therefore deaar from CommonsGuy helped me to get a valid
ADT-friendly project out of an .aar-file. In my case I converted
subsampling-scale-image-view. It took me about an hour to set up ruby on my PC.
Another approach is using android-maven-plugin for Eclipse/ADT as
CommonsGuy writes in his blog.
Yet another approach could be, just cloning the whole desired project
as source from git and import it as "Existing Android project"
The 'aar' bundle is the binary distribution of an Android Library Project. .aar file
consists a JAR file and some resource files. You can convert it
as .jar file using this steps
1) Copy the .aar file in a separate folder and Rename the .aar file to .zip file using
any winrar or zip Extractor software.
2) Now you will get a .zip file. Right click on the .zip file and select "Extract files".
Will get a folder which contains "classes.jar, resource, manifest, R.java,
proguard(optional), libs(optional), assets(optional)".
3) Rename the classes.jar file as yourjarfilename.jar and use this in your project.
Note: If you want to get only .jar file from your .aar file use the
above way.
Suppose If you want to include the manifest.xml and resources with your .jar file means
you can just right click on your .aar file and save it as .jar file directly instead of
saving it as a .zip. To view the .jar file which you have extracted, download JD-GUI(Java Decompiler). Then drag and drop your .jar file into this JD_GUI, you can see the .class file in readable formats like a .java file.
If you are using Gradle for your builds - there is a Gradle plugin which allows you to add aar dependency to your java|kotlin|scala|... modules.
https://github.com/stepango/aar2jar
plugins {
id 'java'
id 'com.stepango.aar2jar' version “0.6” // <- this one
}
dependencies {
compileOnlyAar "com.android.support:support-annotations:28.0.0" // <- Use any AAR dependencies
}

Eclipse Remote Application Platform export resource folder into WAR

I am currently writing a RAP application and would like to export a folder named "repository" containing several files along with the WAR file created by the WAR Product Configuration file. However, the resource folder is never exported into the WAR. I tried setting several BuildPaths in the the Eclipse project, the Manifest.MF and plugin.xml file. None of that worked.
Also, I also still don't know how to get a path to a contained file/folder within the resource folder on disc. I am for example trying to load a Axis2 Repository from that resource folder and my code for that is the following:
context = ConfigurationContextFactory.createConfigurationContextFromFileSystem("/repository/", "/repository/conf/axis2.xml");
yet this always throws the error that the repository could not be found in the filesystem.
Just use WAR Product Tooling plugin to create the .war file, and make sure the repository folder is put into the same project.
http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.rap.help%2Fhelp%2Fhtml%2Fadvanced%2Fdeployment.html

creating META-INF/services folder in Eclipse

I am trying to create an extensible app in Java and chose to use SPI. According to this tutorial I am following, I am supposed to create a specific file in META-INF/services in my jar, but don't know how.
I know how to create jar with META-INF, but can't find a way how to create the services folder and particular file in this folder. (I am using Eclipse)
Thanks for your help.
Since the tutorial is instructing to create it manually rather than using ant or maven or other build tools then just create a folder named META-INF at the root level with a sub-folder named services and a file called MANIFEST.MF.
I am not sure how you plan to execute your jar file, for now you can just put this one line in your MANIFEST.MF:
Manifest-Version: 1.0
For your services folder, you need to create a file named com.example.dictionary.spi.Dictionary with the following one line content as mentioned in the tutorial:-
com.example.dictionary.GeneralDictionary
FYI - META-INF is an internal Java meta directory. Generally, you want to rely on your packaging tool such as ant or maven to build the content of META-INF folder rather than doing it by yourself.
You can see the details on the content of META-INF folder here:-
The following files/directories in the META-INF directory are recognized and interpreted by the Java 2 Platform to configure applications, extensions, class loaders and services:
MANIFEST.MF
The manifest file that is used to define extension and package related data.
INDEX.LIST
This file is generated by the new "-i" option of the jar tool, which contains location information for packages defined in an application or extension. It is part of the JarIndex implementation and used by class loaders to speed up their class loading process.
x.SF
The signature file for the JAR file. 'x' stands for the base file name.
x.DSA
The signature block file associated with the signature file with the same base file name. This file stores the digital signature of the corresponding signature file.
services/
Assuming you are building your jar file with Apache's Ant, you need to add a metainf fileset, or better yet, use a services directive like so
<jar jarfile="pinky.jar">
<fileset dir="build/classes"/>
<service type="javax.script.ScriptEngineFactory"
provider="org.acme.PinkyLanguage"/>
</jar>
If you don't use apache's Ant, then you need to add a file to your jar
jar -uf jarfile.jar META-INF/services/name.of.general.service
which contains the name of the service's implementation
com.mycorp.services.Fooservice
If you are generating the JAR file using Eclipse's "Runnable Jar file export" under the project, select "save as ANT script" and then modify the ant script to pack in the services as described above.

Categories