如何使用 maven pom.xml 创建 jar

Posted

技术标签:

【中文标题】如何使用 maven pom.xml 创建 jar【英文标题】:how to create jar using maven pom.xml 【发布时间】:2012-04-19 15:14:27 【问题描述】:

我需要一个示例 pom 文件来为我的 spring 项目创建一个 jar 文件。创建jar文件时如何指定为资源创建目录和文件夹。我可以使用 pom 创建 jar 文件。但我需要在我的 jar 文件中包含我的 applicationContext.xml。

如何在maven中做到这一点?

【问题讨论】:

【参考方案1】:

解释了如何做到这一点here

这篇文章详细介绍了这个 pom,它使用 maven-jar-plugin 来完成它,我在以前的工作中编写了许多看起来几乎与此相同的 pom。希望这会有所帮助

<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>org.foo</groupId>
  <artifactId>my-app</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>my-app</name>
  <url>http://maven.apache.org</url>
  <build>
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-eclipse-plugin</artifactId>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
      <archive>
        <manifest>
          <addClasspath>true</addClasspath>
          <mainClass>org.foo.App</mainClass>
        </manifest>
      </archive>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
  </plugin>
</plugins>
</build>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

【讨论】:

谢谢。如何创建资源文件夹来放置applicationContext.xml? 见下面@matsev 的回答。 Maven resources plugin 中给出的示例也可能有所帮助。【参考方案2】:

Maven 使用约定优于配置,因此所有标准 Maven 项目都具有相同的folder structure。由于应用程序上下文文件是一种资源,因此您应该将其放入 src/main/resources 文件夹(或其子文件夹之一)中。

【讨论】:

【参考方案3】:
<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.sapta.hr</groupId>
    <artifactId>hrweb</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>Saptalabs HR Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <repositories>
        <repository>
            <id>eclipselink</id>
            <url>http://download.eclipse.org/rt/eclipselink/maven.repo/</url>
        </repository>

        <repository>
            <id>releases</id>
            <name>Releases</name>
            <url>https://oss.sonatype.org/content/repositories/releases</url>
        </repository>
    </repositories>

    <properties>
        <spring.version>3.0.5.RELEASE</spring.version>
        <junit.version>4.11</junit.version>
        <jdk.version>1.7</jdk.version>
        <jetty.version>8.1.8.v20121106</jetty.version>
    </properties>

    <dependencies>

        <!-- Spring 3 dependencies -->

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>$spring.version</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>$spring.version</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>$spring.version</version>
        </dependency>

        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.0.6</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.7</version>
        </dependency>

        <!-- Apache Commons file upload -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.2.2</version>
        </dependency>

        <!-- Apache Commons IO -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>$junit.version</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.9</version>
        </dependency>

        <!-- See http://wiki.eclipse.org/EclipseLink/Maven -->
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.0.0</version>
        </dependency>

        <!-- optional - only needed if you are using JPA outside of a Java EE container -->
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>2.0.0</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.17</version>
        </dependency>

        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20090211</version>
        </dependency>

        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.3</version>
        </dependency>

        <dependency>
            <groupId>com.googlecode.mp4parser</groupId>
            <artifactId>isoparser</artifactId>
            <version>1.0-RC-27</version>
        </dependency>

        <dependency>
            <groupId>com.sapta.hr</groupId>
            <artifactId>hrcommon</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

    </dependencies>
    <build>
        <finalName>saptalabs</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>$jdk.version</source>
                    <target>$jdk.version</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>$jetty.version</version>
            </plugin>

            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <url>http://localhost:8081/manager/text</url>
                    <path>/saptalabs</path>
                    <username>admin</username>
                    <password>admin</password>
                </configuration>
            </plugin>

        </plugins>
    </build>
</project>

【讨论】:

【参考方案4】:

http://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0 com.tridenthyundai.ains 三叉戟 战争 1.0-快照 Trident 现代 Maven Webapp http://maven.apache.org

<!-- <repositories> <repository> <id>eclipselink</id> <url>http://download.eclipse.org/rt/eclipselink/maven.repo/</url> 
    </repository> </repositories> -->

<properties>
    <spring.version>3.0.5.RELEASE</spring.version>
    <junit.version>4.11</junit.version>
    <jdk.version>1.7</jdk.version>
    <jetty.version>8.1.8.v20121106</jetty.version>
</properties>

<dependencies>

    <!-- Spring 3 dependencies -->

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>$spring.version</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>$spring.version</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>$spring.version</version>
    </dependency>

    <!-- Apache Commons file upload -->
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.2.2</version>
    </dependency>

    <!-- Apache Commons IO -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.3.2</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>$junit.version</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.0</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.9</version>
    </dependency>

    <!-- See http://wiki.eclipse.org/EclipseLink/Maven -->
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.0.0</version>
    </dependency>

    <!-- optional - only needed if you are using JPA outside of a Java EE container -->
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>javax.persistence</artifactId>
        <version>2.0.0</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.17</version>
    </dependency>

    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20090211</version>
    </dependency>

</dependencies>
<build>
    <finalName>tridenthyundai</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <source>$jdk.version</source>
                <target>$jdk.version</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>$jetty.version</version>
        </plugin>

        <!-- LOCAL Maven Tomcat Plugin -->
        <!-- <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> 
            <version>2.1</version> <configuration> <url>http://localhost:8080/manager/text</url> 
            <path>/tridenthyundai</path> <username>admin</username> <password>admin</password> 
            </configuration> </plugin> -->

        <!-- PRODUCTION Maven Tomcat Plugin -->
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.1</version>
            <configuration>
                <url>http://mobile.saptalabs.com/manager/text</url>
                <path>/tridenthyundai</path>
                <username>user</username>
                <password>passcode</password>
            </configuration>
        </plugin>

    </plugins>
</build>

【讨论】:

【参考方案5】:

您需要使用maven jar plugin 才能从您的项目中生成 Jar:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
                <mainClass>MainClass</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

查看有关使用 Maven 生成 Jar 文件的更多信息:https://javatutorial.net/create-java-jar-file-with-maven

【讨论】:

以上是关于如何使用 maven pom.xml 创建 jar的主要内容,如果未能解决你的问题,请参考以下文章

如何 配置pom.xml去官方仓库下jar包

如何在maven的pom.xml中添加本地jar包

如何在maven的pom.xml中添加本地jar包

(转)如何在maven的pom.xml中添加本地jar包

如何根据pom.xml文件下载jar包

创建maven项目pom.xml第一行报错