使用编译时环境变量配置 RestApplicationPath

2024-01-03

如文档中所述休息派遣 https://github.com/ArcBees/GWTP/wiki/Rest-Dispatch,其余应用程序路径必须通过常量在 GIN 模块中配置,此处为“/api/v1”:

public class DispatchModule extends AbstractGinModule {
   @Override
   protected void configure() {
       RestDispatchAsyncModule.Builder dispatchBuilder = 
               new RestDispatchAsyncModule.Builder();
       install(dispatchBuilder.build());
       bindConstant().annotatedWith(RestApplicationPath.class).to("/api/v1");   
   }
}

我想根据构建系统根据目标环境(产品、开发等)设置的环境变量以及其他标准(构建工件主要版本...)。

问题是我无法依赖编译时变量。 TextResource/CssResource 和 GWT 都不是延迟绑定 http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsDeferred.html在这里无济于事,因为GWT.create()不能在 GIN 模块中使用。我考虑的另一个选择是使用自定义生成器,但这对于这个非常简单的需求来说似乎太复杂了。

你怎么解决这个问题 ?


如果您使用 Maven 作为构建系统,您可以利用模板 maven 插件 http://mojo.codehaus.org/templating-maven-plugin/生成一个 Java 类,该类将包含 POM 文件中定义的静态变量。您的 GWT 代码将使用该生成的类。

例如,您想要填充BuildConstants类模板

public class BuildConstants {
  // will be replaced by Maven
  public static final String API_VERSION  = "${myapi.version}";
}

并使用 Maven 属性:

<myapi.version>v1</myapi.version>

将被编译为

public class BuildConstants {
  // will be replaced by Maven
  public static final String API_VERSION  = "v1";
}

你可以从你的内部引用这些常量DispatchModule:

bindConstant().annotatedWith(RestApplicationPath.class).to("/api/" + BuildConstants.API_VERSION);   

这是我在项目中使用的 temptting-maven-plugin 的示例配置:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>templating-maven-plugin</artifactId>
  <version>1.0-alpha-3</version>
  <executions>
    <execution>
      <id>filter-src</id>
      <goals>
        <goal>filter-sources</goal>
      </goals>
      <configuration>
        <sourceDirectory>${basedir}/src/main/java-templates</sourceDirectory>
        <outputDirectory>${project.build.directory}/generated-sources/java-templates
        </outputDirectory>
      </configuration>
    </execution>
   </executions>
</plugin>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用编译时环境变量配置 RestApplicationPath 的相关文章

随机推荐