Maven generates jar file with main class setting and dependency libs copy
2012-12-14 06:38
465 查看
When I write a pure java project, I'm afraid of writing deployment doc. Because I need to generate jar file. The most frustrating thing is add other libs into the jar. Maven is a fabulous tool which gives you different ways to deal with dependencies. In
our case, we keep external jars and our jar file separately. We just use maven's plugin, then all things will be done. Let me show you an example:
1. Config main class. In your pom.xml, add a <plugin> inside your <plugins>
2. Config dependencies. Use another plugin, very easy, right?
If you don't specify outputDirectory, the default one is dependency.
Note: If you use Liferay IDE1.5, when you add maven-dependency-plugin it'll give you an error "maven-dependency-plugin (goals "copy-dependencies", "unpack") is not supported by m2e." . It doesn't matter, when you build
jar file, choose to use command line.
3. build .jar file
Go to your project folder where your pom.xml is located, run "mvn clean install". It'll generate lib folder within libs and project.jar inside target folder.
4. run jar file
java -jar project.jar
That's all. That's very easy, right? But I spent almost 1.5 hour on that, util my coworker tells me where to run the jar file. I made a mistake and go to maven repository to run the jar.
![](https://oscdn.geek-share.com/Uploads/Images/Content/201603/899229cfab2c02d614490485cabb781b.gif)
BTW, I ran into a problem in my project. I put my properties file inside src/main/java folder, maven ignore it when it builds jar for me. I tried to use <include>, but it excludes other xml files. The reason is I put the properties file in a wrong place.
I changed to put it under /scr/main/resources folder, maven automatically include it for me.
our case, we keep external jars and our jar file separately. We just use maven's plugin, then all things will be done. Let me show you an example:
1. Config main class. In your pom.xml, add a <plugin> inside your <plugins>
<plugin> <artifactId>maven-jar-plugin</artifactId> <version>2.3.1</version> <executions> <execution> <id>default-jar</id> <phase>package</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <mainClass>com.rujuan.MyMain</mainClass> <classpathPrefix>lib/</classpathPrefix> </manifest> <manifestEntries> <mode>development</mode> <url>${pom.url}</url> </manifestEntries> </archive> </configuration> </plugin>
2. Config dependencies. Use another plugin, very easy, right?
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.6</version> <executions> <execution> <id>copy</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> </execution> </executions> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> </configuration> </plugin>
If you don't specify outputDirectory, the default one is dependency.
Note: If you use Liferay IDE1.5, when you add maven-dependency-plugin it'll give you an error "maven-dependency-plugin (goals "copy-dependencies", "unpack") is not supported by m2e." . It doesn't matter, when you build
jar file, choose to use command line.
3. build .jar file
Go to your project folder where your pom.xml is located, run "mvn clean install". It'll generate lib folder within libs and project.jar inside target folder.
4. run jar file
java -jar project.jar
That's all. That's very easy, right? But I spent almost 1.5 hour on that, util my coworker tells me where to run the jar file. I made a mistake and go to maven repository to run the jar.
![](https://oscdn.geek-share.com/Uploads/Images/Content/201603/899229cfab2c02d614490485cabb781b.gif)
BTW, I ran into a problem in my project. I put my properties file inside src/main/java folder, maven ignore it when it builds jar for me. I tried to use <include>, but it excludes other xml files. The reason is I put the properties file in a wrong place.
I changed to put it under /scr/main/resources folder, maven automatically include it for me.
相关文章推荐
- bulid runnable jar file with dependencies and main class
- maven - package a jar with main class
- hadoop yarn运行jar包 出错 RunJar jarFile [mainClass] args...
- hadoop yarn运行jar包 出错 RunJar jarFile [mainClass] args...
- How to run Java main class and pass application arguments in Maven?
- WCF: Generate Proxy Class and Configuration file for Client
- jar 打包后的文件执行时出现错误:RunJar jarFile [mainClass] args...
- hadoop yarn运行jar包 出错 RunJar jarFile [mainClass] args...
- hadoop yarn运行jar包 出错 RunJar jarFile [mainClass] args...
- Maven下打包成fatjar出现Invalid signature file digest for Manifest main attributes问题解决
- Runjar jarFile [mainClass] args...
- Maven工程产生Jar时Main-Class和Class-Path的配置文件
- Maven 打包可执行 jar( Main-Class)
- maven打好的jar包如何在运行时指定main-class
- hadoop yarn运行jar包 出错 RunJar jarFile [mainClass] args...
- maven编译生成的jar包运行出现 "Failed to load Main-Class manifest attribute from"
- maven打包jar指定 Main-Class
- hadoop yarn运行jar包 出错 RunJar jarFile [mainClass] args...
- How to create a jar file with Maven
- hadoop yarn运行jar包 出错 RunJar jarFile [mainClass] args...