当前位置: 首页 > 知识库问答 >
问题:

从库jar中提取资源文件,同时构建war应用程序

龙高超
2023-03-14
<?xml version="1.0" encoding="UTF-8"?>
<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>com.tugalsan</groupId>
    <artifactId>lib-resource</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>com.google.gwt</groupId>
            <artifactId>gwt-user</artifactId>
            <version>2.9.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>api-gui</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>
    
    <build>
        <resources> 
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.java</include>
                    <include>**/*.gwt.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
        </resources>
        
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-source-plugin</artifactId>
                    <version>3.2.1</version>
                    <executions>
                        <execution>
                            <id>attach-sources</id>
                            <phase>package</phase>
                            <goals>
                                <goal>jar-no-fork</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
    
    <properties>
        <maven.compiler.source>15</maven.compiler.source>
        <maven.compiler.target>15</maven.compiler.target>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

"app-login\pom.xml"

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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>com.tugalsan</groupId>
    <artifactId>app-login</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <properties>
        <spring-boot.version>2.3.3.RELEASE</spring-boot.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.google.gwt</groupId>
                <artifactId>gwt</artifactId>
                <version>2.9.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>net.ltgt.gwt.maven</groupId>
                <artifactId>gwt-maven-plugin</artifactId>
                <inherited>false</inherited>
                <configuration>
                    <launcherDir>${basedir}/app-login-server/target/classes/launcherDir/</launcherDir>
                </configuration>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                    <configuration>
                        <release>15</release>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>net.ltgt.gwt.maven</groupId>
                    <artifactId>gwt-maven-plugin</artifactId>
                    <version>1.0.0</version>
                    <extensions>true</extensions>
                    <configuration>
                        <sourceLevel>11</sourceLevel><!-- gwt cannot compile up -->
                        <failOnError>true</failOnError>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-source-plugin</artifactId>
                    <version>3.2.1</version>
                    <executions>
                        <execution>
                            <id>attach-sources</id>
                            <phase>package</phase>
                            <goals>
                                <goal>jar-no-fork</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>${spring-boot.version}</version>
                    <configuration>
                        <skip>true</skip>
                        <warName>app-login</warName>
                        <outputDirectory>D:\xampp\tomcat\webapps</outputDirectory>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
    <modules>
        <module>app-login-client</module>
        <module>app-login-shared</module>
        <module>app-login-server</module>
    </modules>
</project>

“应用程序登录客户端\pom.xml”

<?xml version="1.0" encoding="UTF-8"?>
<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>

    <parent>
        <groupId>com.tugalsan</groupId>
        <artifactId>app-login</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>app-login-client</artifactId>
    <packaging>gwt-app</packaging>

    <dependencies>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>app-login-shared</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>app-login-shared</artifactId>
            <version>${project.version}</version>
            <classifier>sources</classifier>
        </dependency>
        <dependency>
            <groupId>com.google.gwt</groupId>
            <artifactId>gwt-user</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.gwt</groupId>
            <artifactId>gwt-dev</artifactId>
        </dependency>    
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>lib-bloated</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>net.ltgt.gwt.maven</groupId>
                <artifactId>gwt-maven-plugin</artifactId>
                <configuration>
                    <moduleName>com.tugalsan.app.login.App</moduleName>
                    <moduleShortName>app</moduleShortName>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

“应用程序登录服务器\pom.xml”

<?xml version="1.0" encoding="UTF-8"?>
<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>

    <parent>
        <groupId>com.tugalsan</groupId>
        <artifactId>app-login</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>app-login-server</artifactId>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>app-login-shared</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!-- only needed if you are using GWT-RPC -->
        <dependency>
            <groupId>com.google.gwt</groupId>
            <artifactId>gwt-servlet</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>lib-bloated</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>app-login</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <skip>false</skip>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.2.0</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/target/${project.build.finalName}/</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${basedir}/src/main/resources/public/</directory>
                                    <includes>
                                        <include>**/*.*</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <!-- XXX: We want to exclude app-login-client from 'env-dev' profile, Maven forces us to make a 'env-prod' profile -->
            <id>env-prod</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <dependencies>
                <dependency>
                    <groupId>${project.groupId}</groupId>
                    <artifactId>app-login-client</artifactId>
                    <version>${project.version}</version>
                    <type>war</type>
                    <scope>runtime</scope>
                </dependency>
            </dependencies>
        </profile>
        <profile>
            <id>env-dev</id>
            <activation>
                <property>
                    <name>env</name>
                    <value>dev</value>
                </property>
            </activation>
        </profile>
    </profiles>
</project>

"app-login-share\pom.xml"

<?xml version="1.0" encoding="UTF-8"?>
<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>

    <parent>
        <groupId>com.tugalsan</groupId>
        <artifactId>app-login</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>app-login-shared</artifactId>

    <dependencies>        
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>api-common</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

在最后一分钟,我看到“lib-resource”项目位于“lib-bloated”下,请在下面的“lib-bloated\pom.xml”中找到它

<?xml version="1.0" encoding="UTF-8"?>
<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>com.tugalsan</groupId>
    <artifactId>lib-bloated</artifactId>
    <version>1.0-SNAPSHOT</version>
  
    <dependencies>
        <dependency>
            <groupId>com.google.gwt</groupId>
            <artifactId>gwt-user</artifactId>
            <version>2.9.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>lib-resource</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>lib-boot</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>api-common</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>lib-domain</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>lib-rql</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>lib-login</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>lib-license</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>
    
    <build>
        <resources> 
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.java</include>
                    <include>**/*.gwt.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
        </resources>
        
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-source-plugin</artifactId>
                    <version>3.2.1</version>
                    <executions>
                        <execution>
                            <id>attach-sources</id>
                            <phase>package</phase>
                            <goals>
                                <goal>jar-no-fork</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
    
    <properties>
        <maven.compiler.source>15</maven.compiler.source>
        <maven.compiler.target>15</maven.compiler.target>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

提前谢谢。

共有1个答案

封瑞
2023-03-14

对于不使用Windows Server 2003的用户,以下是答案
我使用了一些实用程序函数,但它将为您提供一个想法,在您的公共类AppServlet实现ServletContextListener类中,
您必须通过以下方式获取应用程序路径。。。

var pathApp = evt.getServletContext().getRealPath("/");

它将返回一个路径,如D:\xampp\tomcat\webapps\app login,然后,您可以构建到lib目录的路径

var pathLibs = Path.of(pathApp.toString(), "WEB-INF", "lib");

然后,您需要根据需要获取子文件。(我有一个实用程序类可以做到这一点)

var pathLibJars_lib = TS_DirectoryUtils.subFiles(pathLibs, "lib-*-1.0-SNAPSHOT.jar", false, false);

然后,您可以将JAR分解到所需的资源文件夹中。(我有另一个unziping实用程序类。它基本上使用7z.exe。)

pathLibJars_lib.forEach(p -> { 
    var label = TS_FileUtils.getNameLabel(p).replace("-1.0-SNAPSHOT", ""); 
    TS_FileZipUtils.unzipFile(p, Path.of(pathApp.toString(), "res", label)); 
});

作为一项改进,需要清理代码,因为所有jar文件的内容都扩展到D:\xampp\tomcat\webapps\app login\res[jarname]文件夹

或者,7z参数可以调整为仅爆炸jar文件中的res文件夹,而不是爆炸所有文件

如果有人能够在maven中自动执行此操作,而不是java,我将不胜感激。

 类似资料:
  • 我在Eclipse中有一个简单的maven项目。应用程序包含文件:src/main/Resources/test.txt.我想读取文件并将其写入输出。它在Eclipse中工作,但在创建jar文件后不能从命令行。如何配置项目以两种方式工作。我错在哪里?不正确的jar文件、java代码、清单、maven配置? 应用程序结构: 应用程序结构 运行: 错误: Java代码: pom.xml: fileRe

  • 我想从我的jar中读取一个资源,如下所示: 在Eclipse中运行它时工作良好,但如果我将它导出到一个jar中,然后运行它,就会出现一个IllegalArgumentException: 我真的不知道为什么,但通过一些测试我发现如果我改变 至 然后它的工作方式相反(它在jar中工作,但不能在eclipse中工作)。 我使用的是Eclipse,我的文件所在的文件夹位于类文件夹中。

  • 启动jar时,控制台会说找不到文件,也没有加载字体。我怎样才能解决这个问题? 我得到了这个密码:

  • 问题内容: 我正在尝试在Java Webstart应用程序中加载许多资源。 我最初尝试使用以下命令加载这些文件: 但这没有用-它引发了IllegalArgumentException。进一步的研究表明,该方法不适用于从jar文件访问资源,因此我需要使用以下方法: 但是,当我从Jnlp使用它运行时,得到奇怪的结果。如果我运行以下命令: 在我的IDE中,我得到了对与错(正如我期望的那样)。从jnlp运

  • 我很难弄清楚这个问题,我几乎什么都试过了。 我希望我的程序从Jar文件中读取两个资源,并且它将在eclipse中运行良好。但是当我从命令提示符运行jar时,它会给出空指针异常。 结构: src/main/java/App。JAVA src/main/resources/properties/application。属性 src/main/resources/spring/applicationCo

  • 当我使用 在intelij运行期间,一切正常。 在jar的案例中,我尝试了: 等等。它们导致正确的路径,例如: > 文件:/D:/Projects/myProject/target/classes/static/assets/config/fileName(对于toExternalForm) /D: /Projects/myProject/target/classes/static/assets/