Java 如何在jar中包含静态文件
我有一个ant构建,它只生成一个装满html页面的jar。我想对maven也这么做。我正在将文件复制到一个新文件夹中,但是这些文件没有被添加到最终的jar中。当我运行命令时Java 如何在jar中包含静态文件,java,maven,ant,pom.xml,Java,Maven,Ant,Pom.xml,我有一个ant构建,它只生成一个装满html页面的jar。我想对maven也这么做。我正在将文件复制到一个新文件夹中,但是这些文件没有被添加到最终的jar中。当我运行命令时jar tf mavenjar.jar 我看不到终端中列出的文件。看起来是这样的: 当我在ant生成的jar上运行相同的命令时,我看到以下内容: 这是我的pom <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3
jar tf mavenjar.jar
我看不到终端中列出的文件。看起来是这样的:
当我在ant生成的jar上运行相同的命令时,我看到以下内容: 这是我的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>
4.0.0
try1
try1
0.0.1-快照
org.apache.maven.plugins
maven jar插件
马文贾尔
${basedir}/moveTheseFiles
${basedir}/newFileLocation
假的
你知道我在将移动的文件包含到jar中时可能遗漏了什么吗 实现这一点有两种方法。在配置上遵循约定-或对其进行配置 约定优先于配置 将静态文件复制到jar中最简单的方法是使用默认文件夹
src/main/resources
。您在那里添加的所有文件都将添加到您的jar文件中。您的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>
注意:似乎您想要创建一个web应用程序。也许您想创建一个war
文件。要实现这一点,只需设置
<packaging>war</packaging>
战争
在pom.xml中。然后,所有web资产(HTML页面、CSS等)应位于
src/main/webapp
下,并将复制到war的根文件夹中。请注意,src/main/resources
中的文件将在产生的战争中复制到WEB-INF/classes
。我实际上按照以下解决方案工作:
我在这个位置src/main/assembly创建了一个bin.xml文件。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>
输出结果如下所示如果您使用此解决方案解决了问题,您可以通过单击绿色复选标记来接受它。这会告诉其他用户此解决方案对您有效,并且更容易看出您不需要任何其他帮助来解决此问题。
<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>
<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>
mvn clean package
cd target
jar tf mavenjar.jar