web-dev-qa-db-ja.com

Mavenpom.xmlを使用してjarを作成する方法

Springプロジェクトのjarファイルを作成するためのサンプルpomファイルが必要です。 jarファイルの作成中にリソース用のディレクトリとフォルダを作成するように指定する方法。 pomを使用してjarファイルを作成できます。しかし、jarファイルにapplicationContext.xmlを含める必要があります。

mavenでそれを行う方法は?

9
Jessie

これを行う方法が説明されています ここ

投稿ではこの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>
10
krystan honour

Mavenは設定より規約を使用するため、すべての標準Mavenプロジェクトは同じ フォルダー構造 になります。アプリケーションコンテキストファイルはリソースであるため、src/main/resourcesフォルダー(またはそのサブフォルダーの1つ)に配置する必要があります。

6
matsev
<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>

http://maven.Apache.org/maven-v4_0_0.xsd "> 4.0.0 com.tridenthyundai.ains tridenthyundai war 1.0-SNAPSHOT Trident Hyundai 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>
0
mohan

プロジェクトからJarを生成するには、 maven 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

0
Johnny