Maven Spring罐包装 [英] Maven Spring jar packaging

查看:113
本文介绍了Maven Spring罐包装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们创建了一个jar(Spring项目),其中包括在另一个Spring项目(战争)中使用Maven

如果我们包含必需的依赖关系,我们可以毫无问题地使用它.

如果我想确保罐子里装满了所有的鱼汤,并使用它们,什么是最好的方法?

为了避免不得不包含我在使用maven程序集插件时尝试过的依赖项 绝对包含这些文件-但是这些文件似乎被忽略了,因为它们仍然是使用项目中的依赖项-为什么有任何建议?

程序集插件的POM详细信息

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.5</source>
                <target>1.5</target>
            </configuration>
        </plugin>
         <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.2.1</version>
    <configuration>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>

    </configuration>
    <executions>
      <execution>
        <id>make-assembly</id> <!-- this is used for inheritance merges -->
        <phase>package</phase> <!-- bind to the packaging phase -->
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

解决方案

您可以使用maven winstone插件创建一个独立的可执行jar,其中包含war,jar等所有依赖项.

添加

<plugins>
  <plugin>
    <groupId>net.sf.alchim</groupId>
    <artifactId>winstone-maven-plugin</artifactId>
    <version>1.2</version>
    <executions>
      <execution>
        <goals>
          <goal>embed</goal>
        </goals>
        <phase>install</phase>
      </execution>
    </executions>
    <configuration>
      <filename>${project.parent.build.finalName}.jar</filename>
    </configuration>
  </plugin>
  ...
</plugins>

会将应用程序的所有模块,wars和jars以及所有需要的依赖项捆绑在一个包含servlet引擎的可执行jar中.

将单个可部署的捆绑包提供给您的Ops团队进行部署真的很不错.

我建议不要创建一个包含所有战争将要使用的依赖项的jar.您可能最终会在类路径中得到同一类的稍有不同版本的多个副本.某些库对此有点冷淡,......(关于冬眠和log4j的想法)气质十足.

Maven做得很不错,可以弄清楚要采用哪些依赖项,并且在发生问题时可以使事情变得更加清晰.创建超级容器时,您会丢失此信息.

如果有充分的理由,我建议您在一个环境中严格控制类路径,例如成熟的J2EE服务器或使用OSGi容器.但是,请小心:它们不是没有(铁)手套才能处理的小猫.

We have created a jar(Spring project) which we are including using Maven in another Spring project (war)

we can use this no problem if we include the required dependancies.

If I wanted to ensure the jar contained all it's depenadancies and used them what is the best way to go about this ?

To avoid having to include the dependancies I have tried in using the maven assembly plugin which definately includes the files - but these appear to be ignored as they are still required as dependancies in the consuming project - any suggestions why?

POM detail for assembly plugin

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.5</source>
                <target>1.5</target>
            </configuration>
        </plugin>
         <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.2.1</version>
    <configuration>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>

    </configuration>
    <executions>
      <execution>
        <id>make-assembly</id> <!-- this is used for inheritance merges -->
        <phase>package</phase> <!-- bind to the packaging phase -->
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

解决方案

You can use the maven winstone plugin to create a standalone executable jar with all dependencies included of the war, the jar etc....

Adding

<plugins>
  <plugin>
    <groupId>net.sf.alchim</groupId>
    <artifactId>winstone-maven-plugin</artifactId>
    <version>1.2</version>
    <executions>
      <execution>
        <goals>
          <goal>embed</goal>
        </goals>
        <phase>install</phase>
      </execution>
    </executions>
    <configuration>
      <filename>${project.parent.build.finalName}.jar</filename>
    </configuration>
  </plugin>
  ...
</plugins>

will bundle all modules of your app, wars and jars, and any needed dependencies in a executable jar which includes a servlet engine.

This works really nice to give a single deployable bundle to your Ops team for deploying.

I would advise against creating a jar which contain all dependencies to be used by the war however. You are likely to end up with multiple copies of slightly different versions of the same classes in the class path. Certain libraries are a bit euhhmmm, ..., temperamentfull about this (hibernate and log4j spring to mind).

Maven does a decent job of figuring out which dependencies to take, and if things break mvn dependency:tree make things a lot clearer. You lose this when you create an uber-jar.

If there are good reasons to do this, I would recommend an environment where you can tightly control the classpaths like a full-blown J2EE server or using an OSGi container. However, be careful what you wish for : these are not kittens to be handled without (iron) gloves.

这篇关于Maven Spring罐包装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆