gradle-在每种构建的一部分之前 /作为将其包含在jar中之前 /作为创建.properties文件

发布于 2025-01-31 17:55:06 字数 1064 浏览 4 评论 0原文

我希望gradle在构建项目时,将版本。

我有这样的任务

task createVersionPropertiesFile()  {
    def outputFile = new File(projectDir, "src/main/java/org/example/lpimport/version.properties")
    outputs.file outputFile
    doLast {
        outputFile.text = 
        """# generated by Gradle
version=$version
revision=${getSvnRevision()}
buildtime=${new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())}
"""
    }
    outputs.upToDateWhen { false }
}

,我将其包括在内:

build.dependsOn createVersionPropertiesFile

当我在eclipse中运行gradle build任务时,将按预期创建文件! ✅

如果我在Eclipse中运行应用程序,则可以通过我的应用程序加载生成的版本文件。 ✅

props.load( Main.class.getResourceAsStream( "version.properties" ) );

但是version.properties文件将不是myProject-1.0中构建myProject-1.0.0.0.0.0.7.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0. .0.zip分发,尽管它位于普通类路径软件包中。 ❌

如何使gradle包括罐子内的即时生成的version.properties

I want Gradle to write a version.properties file into my Java classpath whenever I build the project.

I have a task like this

task createVersionPropertiesFile()  {
    def outputFile = new File(projectDir, "src/main/java/org/example/lpimport/version.properties")
    outputs.file outputFile
    doLast {
        outputFile.text = 
        """# generated by Gradle
version=$version
revision=${getSvnRevision()}
buildtime=${new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())}
"""
    }
    outputs.upToDateWhen { false }
}

And I include it like this:

build.dependsOn createVersionPropertiesFile

When I run the Gradle build task in Eclipse, the file is created as expected! ✅

If I run my application in Eclipse, the generated version.properties file can be loaded by my application. ✅

props.load( Main.class.getResourceAsStream( "version.properties" ) );

But the version.properties file will not be part of the built myproject-1.0.0.jar inside the myproject-1.0.0.zip distribution, although it resides in a normal class path package. ❌

How can I make Gradle include the on-the-fly generated version.properties inside the JAR?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蒲公英的约定 2025-02-07 17:55:06

我通过进行两个更改解决了问题:

  • .properties文件写入src/main/resources而不是src/main/java >:

      def UppoteFile =新文件(projectDir,“ src/main/resources/org/example/lpimport/version.properties”)
     
  • 制作compilejava

    任务取决于我的自定义任务:

      compilejava.depperson createversionpropertiesfile
     

.properties gradle忽略了文件(通过其JAR任务??),如果它位于src之下/main/java

现在它有效,我的构建和包装应用程序可以在启动上显示其版本属性。

I solved the problem by making two changes:

  • Write the .properties file to src/main/resources instead of src/main/java:

    def outputFile = new File(projectDir, "src/main/resources/org/example/lpimport/version.properties")
    
  • Make the compileJava task depend on my custom task:

    compileJava.dependsOn createVersionPropertiesFile
    

It seems the .properties file is ignored by Gradle (by its jar task??) if it resides below src/main/java.

Now it works and my built and packaged application can display its version properties on startup.

鹿童谣 2025-02-07 17:55:06

编辑

当然,一旦我写下这样的答案,我就会学到更好的方法。 Gradle具有

def versionTask = tasks.register("generateVersionProps", WriteProperties) { t ->
  def generatedResourcesDir = project.layout.buildDirectory.dir(["generated", "sources", "thrifty", "src", "main", "resources"].join(File.separator))
  def outputFile = generatedResourcesDir.map {it -> it.file("thrifty-version.properties") }

  t.outputFile(outputFile)
  t.property("THRIFTY_VERSION", VERSION_NAME)
  t.property("KOTLIN_VERSION", libs.versions.kotlin.get())
}

processResources {
  from versionTask
}

原始答案在下面保存下来

我已经完成了几次;可以在这里找到一个很好的(公开)示例: https://github.com/microsoft/thrifty/blob/master/thrifty-gradle-plugin/build.gradle#l45

链接的片段在下面的完整中复制,并提供额外的评论,解释了为什么这样的方法:

def versionTask = tasks.register("generateVersionProps") { t ->
  // Because this is an ephemeral file - generated from other sources -
  // it's best to put it under the build dir, conventionally under 'build/generated'.
  def generatedResourcesDir = project.layout.buildDirectory.dir(["generated", "sources", "thrifty", "src", "main", "resources"].join(File.separator))
  def outputFile = generatedResourcesDir.map {it -> it.file("thrifty-version.properties") }

  // By marking the properties we're using as inputs, Gradle can avoid
  // regenerating this file when nothing has changed.
  t.inputs.property("thrifty-version", VERSION_NAME)
  t.inputs.property("kotlin-version", libs.versions.kotlin.get())

  // Marking our output directory as such tells Gradle what task generates it,
  // saving us from having to manually wrangle 'dependsOn', etc.
  t.outputs.dir(generatedResourcesDir).withPropertyName("outputDir")

  doFirst {
    outputFile.get().getAsFile().with {
      it.delete()
      it << "THRIFTY_VERSION=${VERSION_NAME}\n"
      it << "KOTLIN_VERSION=${libs.versions.kotlin.get()}\n"
    }
  }
}

clean {
  // Make 'clean' aware of the resource we're generating.
  // NB: This works (deleting the task) because we gave the task an output dir.
  delete versionTask
}

// Finally!  We register the output of the version task as part of
// the main resources sourceSet.  This causes our generated dir to
// be included in the build.
sourceSets {
  main {
    resources {
      srcDir versionTask
    }
  }
}

EDIT

Of course, as soon as I write a SO answer, I learn a better way. Gradle has a WriteProperties task that is built in. The original example can be simplified:

def versionTask = tasks.register("generateVersionProps", WriteProperties) { t ->
  def generatedResourcesDir = project.layout.buildDirectory.dir(["generated", "sources", "thrifty", "src", "main", "resources"].join(File.separator))
  def outputFile = generatedResourcesDir.map {it -> it.file("thrifty-version.properties") }

  t.outputFile(outputFile)
  t.property("THRIFTY_VERSION", VERSION_NAME)
  t.property("KOTLIN_VERSION", libs.versions.kotlin.get())
}

processResources {
  from versionTask
}

Original answer preserved below

I've done this a few times; a good (public) example can be found here: https://github.com/microsoft/thrifty/blob/master/thrifty-gradle-plugin/build.gradle#L45

The linked snippet is reproduced in full below, with extra comments explaining why it's this way:

def versionTask = tasks.register("generateVersionProps") { t ->
  // Because this is an ephemeral file - generated from other sources -
  // it's best to put it under the build dir, conventionally under 'build/generated'.
  def generatedResourcesDir = project.layout.buildDirectory.dir(["generated", "sources", "thrifty", "src", "main", "resources"].join(File.separator))
  def outputFile = generatedResourcesDir.map {it -> it.file("thrifty-version.properties") }

  // By marking the properties we're using as inputs, Gradle can avoid
  // regenerating this file when nothing has changed.
  t.inputs.property("thrifty-version", VERSION_NAME)
  t.inputs.property("kotlin-version", libs.versions.kotlin.get())

  // Marking our output directory as such tells Gradle what task generates it,
  // saving us from having to manually wrangle 'dependsOn', etc.
  t.outputs.dir(generatedResourcesDir).withPropertyName("outputDir")

  doFirst {
    outputFile.get().getAsFile().with {
      it.delete()
      it << "THRIFTY_VERSION=${VERSION_NAME}\n"
      it << "KOTLIN_VERSION=${libs.versions.kotlin.get()}\n"
    }
  }
}

clean {
  // Make 'clean' aware of the resource we're generating.
  // NB: This works (deleting the task) because we gave the task an output dir.
  delete versionTask
}

// Finally!  We register the output of the version task as part of
// the main resources sourceSet.  This causes our generated dir to
// be included in the build.
sourceSets {
  main {
    resources {
      srcDir versionTask
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文