how to edit pom.xml and create a .jar file - java

i have a .jar file which has pom.xml .I want to add a dependency to it .I tried editing the pom.xml ,added the dependency ,zipped the file and renamed it to .jar .but when I include the .jar file it gives me the following error "error reading .jar,error in opening .zip file". I am using BlueJ.please help:(
my pom.xml file is
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>javaee-pom</artifactId>
<version>4.0</version>
</parent>
<name>GlassFish javaee.jar </name>
<artifactId>javaee</artifactId>
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Class-Path>../modules/javax.servlet-api.jar ../modules/endorsed/javax.annotation-api.jar ../modules/javax.ejb-api.jar ../modules/javax.transaction-api.jar ../modules/javax.enterprise.deploy-api.jar ../modules/javax.management.j2ee-api.jar ../modules/javax.resource-api.jar ../modules/javax.security.auth.message-api.jar ../modules/javax.security.jacc-api.jar ../modules/webservices-osgi.jar ../modules/jaxb-osgi.jar ../modules/endorsed/jaxb-api-osgi.jar ../modules/endorsed/webservices-api-osgi.jar ../modules/javax.xml.rpc-api.jar ../modules/javax.xml.registry-api.jar ../modules/javax.mail.jar ../modules/javax.faces.jar ../modules/javax.servlet.jsp-api.jar ../modules/javax.el.jar ../modules/javax.servlet.jsp.jstl-api.jar ../modules/javax.persistence.jar ../modules/javax.jms-api.jar ../modules/bean-validator.jar ../modules/weld-osgi-bundle.jar ../../mq/lib/jaxm-api.jar ../modules/javax.ws.rs-api.jar ../modules/javax.json.jar ../modules/javax.websocket-api.jar ../modules/javax.enterprise.concurrent-api.jar ../modules/javax.batch-api.jar</Class-Path>
<GlassFish-ServerExcluded>true</GlassFish-ServerExcluded>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency></project>
i have already added the dependency i want.zipped the folder and renamed it to jar.but it gives me error as mentioned above.

Related

Getting "Unsatisfied Link" Error When Trying to Run Java Application Using Maven and WindowBuilder

I am trying to test the basic structure of my GUI application that I started using WindowBuilder. I am also using Maven so that I can just download the dependencies from their repository.
I don't have any errors when I do a "Maven Clean" or a "Maven Install", but when I try to do "Run As--->Java Application", I get the following error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no swt-win32-3044 in java.library.path
swt-win32-3044 was what I found in the Maven Repository to satisfy the requirements for the WindowBuilder. I don't have any errors in my POM file, but here is what I have...
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>blah.blah.blah</groupId>
<artifactId>blah</artifactId>
<version>1.0.0</version>
<name>blah</name>
<dependencies>
<dependency>
<groupId>swt</groupId>
<artifactId>swt-win32</artifactId>
<version>3.0m8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemProperties>
<property>
<name>java.library.path</name>
<value>${project.build.directory}</value>
</property>
</systemProperties>
</configuration>
</plugin>
</plugins>
</build>
Any help would be greatly appreciated. Thanks.
So I changed the POM around a little bit. I tried a different Maven dependency for WindowBuilder and changed my syntax for defining the maven-surefire-plugin.
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>blah.blah.blah</groupId>
<artifactId>blah</artifactId>
<version>1.0.0</version>
<name>blah</name>
<dependencies>
<dependency>
<groupId>org.eclipse.swt</groupId>
<artifactId>org.eclipse.swt.win32.win32.x86_64</artifactId>
<version>4.3</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
It now launches the Java application window as expected.

Obtaining systemPath from properties file

I'm very new to maven, so kindly bear with me.
i have a maven project with almost 100 dependencies all of which are present in my local system. So I'm adding them to my classpath via pom.xml with <scope> being system and <systemPath> giving path to the said dependencies. A rough structure of my pom.xml looks like this:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>MAVEN_APP</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MAVEN_APP</name>
<properties>
<jdk.version>1.8</jdk.version>
</properties>
<dependencies>
<dependency>
<groupId>jar1</groupId>
<artifactId>dependency1</artifactId>
<version>0.9.5</version>
<scope>system</scope>
<systemPath>path/to/directory/outside/project/directory/jar1Name.jar</systemPath>
</dependency>
......
<dependency>
<groupId>jar100</groupId>
<artifactId>dependency10</artifactId>
<version>1.9.7</version>
<scope>system</scope>
<systemPath>path/to/directory/outside/project/directory/jar10Name.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
This includes all the jars into my classpath and everything is running fine.
But i would like to keep the path/to/directory/outside/project/directory/ inside my app.propeties file and pom.xml should fetch this path from the properties file.
Is there a way in maven to do this.
Could somebody help me here. Thanks in advance.

Spring application context unable to find property files under maven resources folder

I am using maven in my project & I've put database.properties file under
maven resources folder.
I am accessing it in spring application context
<context:property-placeholder location="classpath:database.properties" />
But I don't understand why I am getting this file not found error on server start.
Could not load properties; nested exception is
java.io.FileNotFoundException: class path resource
[database.properties] cannot be opened because it does not exist
To my knowledge whatever resource is put under resources folder it is added to the classpath automatically by maven.
pom.xml
<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.myapp</groupId>
<artifactId>myapp</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>myapp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>myapp</finalName>
</build>
</project>
NOTE: Seems like there is some problem with the M2E plugin of eclipse luna. Executed the same code in Juno & it worked fine.
That's not enough to build a deployable war file with maven. You need at least the war plugin. There's a thorough tutorial here:
http://crunchify.com/how-to-create-a-war-file-from-eclipse-using-maven-plugin-apache-maven-war-plugin-usage/
Your pom will look something like below, and you'll need to run mvn clean install against that.
Also I see you have a spring app, where are the spring dependencies in the pom file?
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>CrunchifyTutorial</groupId>
<artifactId>CrunchifyTutorial</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
</project>

How to set up the main class header in the manifest

I've created a simple project in Java Maven. I can run this project by:
1:
java -cp target/MavenTestApp-1.0-SNAPSHOT.jar org.koushik.javabrains.App
But I would like to run it by:
2:
java -jar target/MavenTestApp-1.0-SNAPSHOT.jar
To run it by second method I need to change manifest file. I am using maven so I think that in this case I need to change the pom.xml file. My pom.xml file is:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.koushik.javabrains</groupId>
<artifactId>MavenTestApp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MavenTestApp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
I need to add something like this:
<mainClass>org.koushik.javabrains.App</mainClass>
But I not pretty sure where and how to do that? Could you also tell me if this line is good:
2:
java -jar target/MavenTestApp-1.0-SNAPSHOT.jar
Maybe at the end of this line there should be something or it is sufficient? Thanks.
Take a look at the Maven jar plugin documentation.
You will need to configure something similar in your .pom:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
...
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>org.koushik.javabrains.App</mainClass>
</manifest>
</archive>
</configuration>
...
</plugin>
</plugins>
</build>
...
</project>

Building a maven project for deployment

Hello all I'm pretty new to using maven to build Java project so I'm sure the solution to my problem will be quite easy. I have installed the Eclipse Maven plugin and created a maven project. I now wish to package this project and distribute it as a runnable jar file. However when I run
mvn package
and then try to run the jar file I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: main/Formatter
The pom for this particular file looks like so:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>GROUP</groupId>
<artifactId>pcap.csv.xml</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>net.sf.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
</project>
Any help would be greatly appreciated.
You need to specify the main class in the jar plugin:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>main.Formatter</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
If you want to distribute a single jar you need to use a single-jarifier (fatjar, jarjar, onejar, etc.)

Categories

Resources