簡體   English   中英

使用 maven 但非 Java 應用程序的吊臂

[英]Using a jib for maven but non-Java application

我正在創建一個項目並使用 Jib 創建容器並將它們推送到 ECR。 It is a multi-module maven project which has 3 sub-modules, 2 of them are standard java spring-boot project which is woking fine with Jib and the other one is an npm project which build using mvn. 這是它的pom。

<?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>

    <artifactId>my-search-frontend</artifactId>
    <packaging>pom</packaging>
    <version>1.11.0-SNAPSHOT</version>
    <name>my search frontend</name>
    <description>my search frontend</description>

    <parent>
        <artifactId>my-search</artifactId>
        <groupId>com.regalo.my</groupId>
        <version>1.11.0-SNAPSHOT</version>
    </parent>

    <build>
        <finalName>my-search-frontend</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <filesets>
                        <fileset>
                            <directory>node_modules</directory>
                            <includes>
                                <include>**/*</include>
                            </includes>
                            <followSymlinks>false</followSymlinks>
                        </fileset>
                        <fileset>
                            <directory>build</directory>
                            <includes>
                                <include>**/*</include>
                            </includes>
                            <followSymlinks>false</followSymlinks>
                        </fileset>
                    </filesets>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <executions>
                    <execution>
                        <id>npm-install</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <executable>npm</executable>
                            <arguments>
                                <argument>install</argument>
                            </arguments>
                            <workingDirectory>${project.basedir}</workingDirectory>
                        </configuration>
                    </execution>
                    <execution>
                        <id>npm-run-build</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <executable>npm</executable>
                            <arguments>
                                <argument>run</argument>
                                <argument>build</argument>
                            </arguments>
                            <workingDirectory>${project.basedir}</workingDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <configuration>
                    <skipDocker>${skip.docker}</skipDocker>
                    <imageTags>
                        <imageTag>${project.version}</imageTag>
                        <imageTag>latest</imageTag>
                    </imageTags>
                    <dockerDirectory>${project.basedir}/docker</dockerDirectory>
                    <resources>
                        <resource>
                            <targetPath>/build</targetPath>
                            <directory>${project.basedir}/build</directory>
                            <include>**/*</include>
                        </resource>
                        <resource>
                            <targetPath>/build</targetPath>
                            <directory>${project.basedir}</directory>
                            <includes>
                                <include>index.html</include>
                            </includes>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.google.cloud.tools</groupId>
                <artifactId>jib-maven-plugin</artifactId>
                <configuration>
                    <to>
                        <image>${docker.repository.host}/${project.artifactId}:${project.version}</image>
                    </to>
                    <!-- <skip>${skip.docker}</skip> -->
                    <extraDirectories>
                        <paths>
                            <path>
                                <from>${project.basedir}</from>
                                <into>/build</into>
                            </path>
                        </paths>
                    </extraDirectories>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Spotify one 是我們之前的做法,現在我們正在轉向 Jib。 但是我正在為這個應用程序構建解決問題,

    [ERROR] Failed to execute goal com.google.cloud.tools:jib-maven-plugin:2.7.1:build (default-cli) on project my-search-frontend: Obtaining project build output files failed; make sure you have compiled your project before trying to build the image. 
(Did you accidentally run "mvn clean jib:build" instead of "mvn clean compile jib:build"?): /home/cyrex/Documents/Sourcecode/my-search/my-search-frontend/target/classes -> [Help 1]

應用程序的項目結構

在此處輸入圖像描述

對此的幫助將不勝感激。

The Jib Maven and Gradle plugins are for Java apps, and the error message is complaining that there are no compiled .class files in your NPM module. However, technically, you may be able to make Jib build an NPM image with some tricks (for example, put a dummy DummyClass.java under src/main/java to bypass the error, override <container><entrypoint> to not execute java ,使用<extraDirectories>放置任意文件,設置<from><image>以使用非 JRE 基礎映像等)。 您可能還需要使用Jib Layer-Filter 擴展來刪除文件。 但是,由於 Jib 確實是為 Java 應用程序量身定制的,因此我不能真正推薦它。

僅供參考,Jib 可以通過Jib 擴展框架進行高度定制和擴展,因此理論上您可以編寫一個 NPM Jib 擴展來涵蓋這個高度專業化的工作流程(並與 Jib 社區共享)。

此外,對於非 Java 應用程序和非 Maven 工作流,我們還有其他 Jib 解決方案: Jib Core (Java 庫)和Jib CLI Jib CLI 可能在這種情況下工作,盡管它不是 Maven 插件。 (我不知道這是否是個好主意,但僅供參考,似乎有幾種方法可以在 Maven 中運行任意命令。)


與此相關的是, 本文展示了一個使用 Jib 層過濾器擴展來啟用 Jib 不支持的用例的示例。

JIB 僅適用於 java 項目。 它處理在優化層中安裝 Java 運行時和依賴項。 但它無法創建 NPM docker 圖像。

所以你只能為 java 模塊啟用 jib-maven-plugin maven 插件。

對於 NPM,您應該堅持使用 spotify docker-maven-plugin 或類似的東西。 或者試試fabric8

如果有人仍在展望未來,請提供替代方案(這是我的谷歌搜索的第一次命中):

您可以將 jib 插件設置為使用 packaged.jar 文件,如果您使用mvn package會自動生成,如果不存在代碼則非常小(~2kb)

還要確保您的項目沒有父項或依賴項,否則 jib 會將庫復制到映像中。

<build>
    <plugins>
        <plugin>
            <groupId>com.github.eirslett</groupId>
            <artifactId>frontend-maven-plugin</artifactId>
            <version>1.12.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>install-node-and-npm</goal>
                    </goals>
                </execution>
                <execution>
                    <goals>
                        <goal>npm</goal>
                    </goals>
                </execution>
                <execution>
                    <goals>
                        <goal>npm</goal>
                    </goals>
                    <configuration>
                        <arguments>run build</arguments>
                    </configuration>
                </execution>
            </executions>
            <configuration>
                <nodeVersion>v16.9.1</nodeVersion>
            </configuration>
        </plugin>

        <plugin>
            <groupId>com.google.cloud.tools</groupId>
            <artifactId>jib-maven-plugin</artifactId>
            <version>2.7.1</version>
            <configuration>
                <containerizingMode>packaged</containerizingMode>
                <extraDirectories>
                    <paths>
                        <path>
                            <from>build</from>
                            <into>/var/www/html</into>
                        </path>
                    </paths>
                </extraDirectories>
                <container>
                    <entrypoint>INHERIT</entrypoint>
                </container>
                <from>
                    <image>nginx</image>
                </from>
                <to>
                    <image>THE IMAGE NAME:${project.version}</image>
                </to>
            </configuration>
        </plugin>
    </plugins>
</build>

創建它: mvn clean package jib:dockerBuild

使用潛水檢查是否添加了不必要的文件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM