如何将环境变量传递给部署在tomcat中的React应用?

时间:2020-06-15 15:33:31

标签: java reactjs maven tomcat8

我有一个使用 Webpack 的React应用,其中我添加了 web.xml pom.xml 文件以在 tomcat 中部署应用。 现在,我想将环境变量传递给我的react应用程序,就像将其传递给Java应用程序一样。但是我找不到任何将环境变量传递给我的应用程序的方法。 这是我的pom.xml文件:

http://maven.apache.org/xsd/maven-4.0.0.xsd“>

<modelVersion>4.0.0</modelVersion>
<groupId>org.pom-co</groupId>
<artifactId>pom-co</artifactId>
<version>3.0.0</version>
<packaging>war</packaging>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <npm.output.directory>dist</npm.output.directory>
</properties>

<build>
    <finalName>${project.artifactId}</finalName>
    <plugins>

        <!-- display active profile in compile phase -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-help-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
                <execution>
                    <id>show-profiles</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>active-profiles</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <!-- Standard plugin to generate WAR -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <webResources>
                    <resource>
                        <directory>${npm.output.directory}</directory>
                    </resource>
                </webResources>
                <webXml>${basedir}/web.xml</webXml>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.3.2</version>
            <executions>
                <!-- Required: The following will ensure `npm install` is called
                     before anything else during the 'Default Lifecycle' -->
                <execution>
                    <id>npm install (initialize)</id>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                    <phase>initialize</phase>
                    <configuration>
                        <executable>npm</executable>
                        <arguments>
                            <argument>install</argument>
                        </arguments>
                    </configuration>
                </execution>
                <!-- Required: The following will ensure `npm install` is called
                     before anything else during the 'Clean Lifecycle' -->
                <execution>
                    <id>npm install (clean)</id>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                    <phase>pre-clean</phase>
                    <configuration>
                        <executable>npm</executable>
                        <arguments>
                            <argument>install</argument>
                        </arguments>
                    </configuration>
                </execution>

                <!-- Required: This following calls `npm run build` -->
                <execution>
                    <id>npm run build (compile)</id>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                    <phase>compile</phase>
                    <configuration>
                        <executable>npm</executable>
                        <arguments>
                            <argument>run</argument>
                            <argument>build</argument>
                        </arguments>
                    </configuration>
                </execution>

            </executions>

            <configuration>
                <environmentVariables>
                    <CI>true</CI>
                    <!-- The following parameters create an NPM sandbox for CI -->
                    <NPM_CONFIG_PREFIX>${basedir}/npm</NPM_CONFIG_PREFIX>
                    <NPM_CONFIG_CACHE>${NPM_CONFIG_PREFIX}/cache</NPM_CONFIG_CACHE>
                    <NPM_CONFIG_TMP>${project.build.directory}/npmtmp</NPM_CONFIG_TMP>
                </environmentVariables>
            </configuration>
        </plugin>
    </plugins>
</build>

<profiles>
    <profile>
        <id>local</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>

                    <configuration>
                        <environmentVariables>
                            <REACT_APP_CUSTOM_VARIABLE>http://localhost:3001/</REACT_APP_CUSTOM_VARIABLE>
                        </environmentVariables>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

我想通过process.env.REACT_APP_CUSTOM_VARIABLE访问我的应用程序中REACT_APP_CUSTOM_VARIABLE以上的内容,而且我想访问REACT_APP_CUSTOM_VARIABLE的值,例如

<environmentVariables>                                    
<REACT_APP_CUSTOM_VARIABLE>
env.CUSTOMVARIABLE
</REACT_APP_CUSTOM_VARIABLE>
</environmentVariables>

我希望这里是因为我可以像这样打包应用程序时传递环境变量:

mvn package -DCUSTOMVARIABLE=somevalue

或在tomcat环境中将其提供为JAVA_OPTS ='-DCUSTOMVARIABLE = somevalue'。

不确定如何完成。 请帮助!!!

0 个答案:

没有答案