[英]how to include static files in jar
i have an ant build that just generates a jar full of html pages. 我有一个蚂蚁建立,只生成一个罐子的html页面。 i would like to do the same with maven.
我想对Maven做同样的事情。 im currently copying the files over to a new folder but the files are not being added to the final jar.
我目前正在将文件复制到新文件夹中,但是文件没有添加到最终的jar中。 when i run the command
当我运行命令
jar tf mavenjar.jar
i dont see the files listed in my terminal. 我看不到终端中列出的文件。 it looks like so: click here
看起来像这样: 单击此处
when i run the same command on the ant produced jar, i see the following: click here here's my pom 当我在生产的蚂蚁罐子上运行相同的命令时,我看到以下内容: 单击此处是我的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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>try1</groupId>
<artifactId>try1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
</plugins>
<finalName>mavenjar</finalName>
<resources>
<resource>
<directory>${basedir}/moveTheseFiles</directory>
<targetPath>${basedir}/newFileLocation</targetPath>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
any idea on what i may be missing to include the moved files to jar? 关于我可能缺少将移动文件包含到jar中的任何想法?
There are two ways to achieve this. 有两种方法可以实现此目的。 Following Convention over Configuration - or configuring it.
遵循约定优于配置-或对其进行配置。
Convention over Configuration 约定优于配置
The easiest way to copy static files into your jar, is using the default folder src/main/resources
. 将静态文件复制到jar中的最简单方法是使用默认文件夹
src/main/resources
。 All files you add there, will be added into your jar file. 您在此处添加的所有文件都将添加到您的jar文件中。 Your pom.xml can be reduced to this:
您的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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>try1</groupId>
<artifactId>try1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<finalName>mavenjar</finalName>
</build>
</project>
Non-default folder 非默认文件夹
When you really need to have your files in another folder, your configuration is nearly fine. 当您确实需要将文件保存在另一个文件夹中时,配置就可以了。 Just the
includes
part is missing. 仅
includes
部分丢失。 You have to tell maven to include all files in the specified folder. 您必须告诉maven将所有文件包括在指定的文件夹中。
<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>try1</groupId>
<artifactId>try1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<finalName>mavenjar</finalName>
<resources>
<resource>
<directory>moveTheseFiles</directory>
<includes>
<include>**/*</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
Note: It seems as if you wanted to create a web application. 注意:似乎要创建一个Web应用程序。 Maybe you want to create a
war
file. 也许您想创建一个
war
文件。 To achieve this, you can simply set 为此,您只需设置
<packaging>war</packaging>
in your pom.xml. 在您的pom.xml中。 Then all web assets (HTML-pages, CSS, ...) should be located under
src/main/webapp
and will be copied into the war's root folder. 然后,所有网络资产(HTML页面,CSS等)都应位于
src/main/webapp
,并将其复制到war的根文件夹中。 Be aware, that files in src/main/resources
will be copied to WEB-INF/classes
in the resulting war. 请注意,在最终的战争中,
src/main/resources
中的文件将被复制到WEB-INF/classes
中。
i actually got it work following this solution: Create zip in maven with additional files next to the jar 我实际上是按照以下解决方案工作的: 在Maven中创建zip,在jar旁边添加其他文件
i created a bin.xml file in this location src/main/assembly. 我在src / main / assembly这个位置创建了一个bin.xml文件。 the bin.xml looks like this
bin.xml看起来像这样
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>bin</id>
<baseDirectory>/</baseDirectory>
<formats>
<format>jar</format>
</formats>
<fileSets>
<fileSet>
<directory>${basedir}/dist/newLocation</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
</assembly>
and my pom.xml looks like so: 我的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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>try1</groupId>
<artifactId>try1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<finalName>mavenjar</finalName>
<resources>
<resource>
<directory>${basedir}/WWW</directory>
<targetPath>${basedir}/dist/newLocation</targetPath>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/bin.xml</descriptor>
</descriptors>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- append to the packaging phase. -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
in the terminal i run 在终端我跑
mvn clean package
cd target
jar tf mavenjar.jar
and the output looks like this click here 和输出如下所示,请点击这里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.