I have created some supportive java classes for help and speed-up the development. Now I want to build a jar file ( hibernate jar files like that) collecting these java classes. then I can add that jar file to other project and use it.. how can I possible to do that.? or any other suggestions?
you can create a jar file by using the following commands
jar -cvf
here c indicates create, v for verbose & f stands for files to be included inside your jar.
If your class files includes main class you need to include manifest file to mention the main class name.
Note: If you want an standalone executable jar which includes another jar files then you there might be a problem while executing the standalone jar because the added libraries will not be placed in classpath at the time of execution. You can take help of eclipse IDE which will create a jar file which includes the jar file executes successfully.
If you are trying to build a JAR it depends on your IDE.
In Netbeans for example you can run the clean and build command whereafter your JAR will be placed in PROJECT_FOLDER>Dist
If you are using eclipse then try installing fatjar plugin. Once installed then right click on your java project and you can build it as jar. If you need you can include the dependencies too in the result jar.
First Convert your .java file to .class with the command line.
javac ClassName.java
Next, create a JAR file containing the ClassName.class file. Type the following in your command window:
jar cvf ClassName.jar ClassName.class
This creates a JAR file, ClassName.jar, and places the ClassName.class file inside it.
For further reference See the Documentation
You could use maven to bundle your jar, deploy it to a private repo like nexus, then pull down the jar in the second project with maven dependency management. Technically you don't need nexus, you could just build the jar and have it go to your local .m2 repo. Then when the second project gets build it will pull the jar from your local .m2 repo. You can achieve this by running mvn install on the jar project.
Easiest steps
Setup your first project, the jar project, to be built with maven. You will need to setup a pom.xml in the project root. You should also follow a standard folder layout. You don't need to use this layout for maven but it makes things easier. Maven will allow you to override all of these locations in the build element of the pom.xml.
Next search google for how to setup a basic pom.xml. Set your first project to package type jar. Here's an example pom.xml of what it may look like for your jar project. Don't paste the ... they are just there as placeholders because I don't know the details of how you want to build your project.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.your.package.base</groupId>
<artifactId>your-jar-project-name</artifactId>
<packaging>jar</packaging>
<name>your-jar-project-name</name>
<version>1.00</version>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
...
</dependencies>
<build>
...
</build>
</project>
After you have the pom.xml in the root, cd to that directory and run mvn install. This will build the jar and place it in your local .m2 repo. Next you will need to setup a pom.xml in the project that needs the jar project. Insert this in the dependencies section of the project that needs to use the jar.
<dependency>
<groupId>com.your.package.base</groupId>
<artifactId>your-jar-project-name</artifactId>
<version>1.00</version>
</dependency>
Good luck and have fun!
Related
So I need to connect a library to my repo in IntelliJ, so I can test the library as it interacts with my main project. And I got this command from a member on my team: "So you need to change .java files in entity manager (which is the library) repo and do mvn clean package to generate the jar file locally, the use that jar in your project repo.
Then create dummy folder in your project with the same package name and copy those .java files from entity manager."
What does change the java files mean? What about "use that jar"? I'm so confused.
Use "mvn install" instead of package. This puts the library into your local .m2 repo and makes it available to your project in idea
If you do mvn clean package it produces a .jar file in target/ folder. Use that dependency to your project if they both share a common parent pom.xml
If you do mvn clean install it produces the jar file in target/ folder + copies the .jar file to your local m2 folder. That way you can use that .jar file to any project locally. Just use the with matching version, artifact and groupId whereever you need to use that jar file.
For both options above, you need to define sth like below in your pom.xml
<dependency>
<groupId>com.my.library</groupId>
<artifactId>mylib-artifact</artifactId>
<version>1.VERSION</version>
</dependency>
Not recommended:
You can also put the .jar file to your project (without doing mvn clean install) See: http://blog.gtiwari333.com/2016/12/maven-use-local-jar-without-installing.html
Or manually add the .jar to your project as project dependency using your IDE's project configuration
I wrote a small OpenGL library for private use.
It is based on Lwjgl. I am programming with eclipse.
Now I want to export this library to have one JarFile, which
I can add to the build path of a project and the user can
only use the library and not lwjgl. So I want to keep lwjgl and the
native files in the build path but export it with my library as one jar.
How could I achieve this?
You can consider using maven to help manage your project life cycle. While there are many other tools out there, maven has been one of the more widely used so if you encountered any difficulties, it won't be hard to find solutions.
Maven can be used to build and package your artifacts, and manage your dependencies (in this case lwjgl and other dependencies lwjgl needs). Since you are already using Eclipse, you can easily use it to create a maven project. (Refer this post here for guidance). From there, Eclipse will help to manage all your build/class paths.
Project Structure
After you create your maven project in Eclipse, you will see that under the project root folder, there is at least:
a pom.xml file. Maven uses this file to determine anything and everything about your project including dependencies.
a src/main folder. This is where you will keep all your Java source codes.
a src/test folder. This is where you will keep all your test codes.
Managing Your Dependencies
The next step involves modifying your pom.xml to specify lwjgl as a dependency. To do so, add the following dependency configuration to your pom.xml
<dependency>
<groupId>org.lwjgl.lwjgl</groupId>
<artifactId>lwjgl</artifactId>
<version>2.9.1</version>
</dependency>
Note that this <dependency> configuration should be added to a parent <dependencies> section.
This will download the main lwjgl.jar (version 2.9.1) and the natives for Windows, Linux and Mac OSX from the Maven Central Repository into your project (so you don't have to manage it manually, say in a separate lib folder).
Export Your Project As A JAR
If all went well, you will be able to build your project by navigating to your project root from command line (you can do this in Eclipse too), and issue the command
mvn clean install
which will build your Java codes, execute your unit tests suite (if any), download any dependencies specified in your pom.xml, and generate a JAR file named after your project in the target folder.
To verify, unzip the JAR file and you should be able to find lwjgl.jar along with any other dependencies in one of the folders.
Hope this will get you started.
EDIT:
Building Your Project
If your target folder remains empty after executing mvn clean install, try include this build plugin configuration in your pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
</plugin>
</plugins>
</build>
I have downloaded a project from git. Its a web project using maven. I have eclipse in my machine and also maven is there. Now I want to create a war file from this project for deploying in Tomcat.
Any idea how I can do it. Command line will be a prefer option. I searched in Google but in most of the places it is asking to create a new Maven project or I am missing something.
Thank you
Just add <packaging>war</packaging> to your pom.xml, and run mvn package from the root of your project (i.e. where the pom.xml resides). If everything will be successful, you'll get a war file in the target directory. See: http://maven.apache.org/pom.html#Maven_Coordinates
I have added to my pom.xml a section that specifies the mainClass and allows it to essentially create an executable jar. I have included a bunch of dependencies that maven manages as well. It executes fine, but fails to run when it gets to a section of code that needs to know the location of a jar package that was made inhouse by somebody (i.e., not from Maven). In the project in eclipse I had put the jar in src/lib and my code is in src/main/java. I had to select properties and Java Build Path and specify there the src/lib location for the jar to get it to even compile. However, trying to run java -jar name.jar has it fail and complain because it fails to import the classes from the src/lib jar. Since it is not a maven thing, how to I make sure this is a dependency for this project and that it is seen on the project's classpath?
The thing with maven is that maven has to control all of the dependencies and that includes this jar you want to reference. That doesn't mean that you have to build that other jar using maven, you could mvn install it in your local repository or use a tool like Artifactory to put it in a private remote repository. I know that installing regular jars via Artifactory creates a pom file for the jar and from then on you can treat the jar like any other maven dependency.
I have a project which as part of the build process creates an XMLBeans jar file (stbSchemas.jar) which I want to include and reference in this project.
Is this the best way to go about this (Single project) or should I have a child project which is built from the parent project?
I am building this using Maven2 inside Eclipse. Is there a better way to do this so that I can maintain the integrity of the projects and stability of the builds.
Hmm. If I understand correctly, you're saying that the you need to reference the stbSchemas.jar in your project in order to build some code that DOESN'T go into the stbSchemas.jar file. If that's a correct assumption, then I think you should probably do a little bit of refactoring so that you have something like this:
1) a Maven parent pom file at the top level of the project. That parent pom has a modules section that would reference the 2 modules from step 2. E.G:
<modules>
<module>stbSchemas</module>
<module>core</module>
</modules>
2) Split the code into clean modules. stbSchemas would be the first, your other code would be a 2nd module. Each of those modules gets its own pom.xml file that would reference the parent pom file like this:
<parent>
<groupId>parent.group</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
The second module's pom.xml file would also need a dependency section for stbSchemas.jar, like this:
<dependency>
<groupId>my.group</groupId>
<artifactId>stbSchemas</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
Then, when you build your second module, it'll pick up stbSchemas.jar.
3) Once you've got that (or something similar) setup in Maven, you can use the maven eclipse plugin (mvn eclipse:eclipse) to generate a correct eclipse classpath for the whole project. You can then refresh the project in Eclipse, and that will put stbSchemas.jar on the classpath for you.
My suggestion is that, when you build the jar using Maven, lets have one folder say "runtime" in the project where you want to use this jar. And through maven only copy the jar to runtime of the project as soon as the jar is created so whenever you will have a new build, you will have the latest jar in the runtime folder.
And now make your project to refer to the jar from the runtime folder. by adding it in its build path.
Hope this helps
write a script to copy the jar to the desired folder and edit the xml file in .settings folder of the project to include the jar as a reference through the same script or add it in its buildpath at runtime.