我之前在阅读InfoQ关于面向复合编程的文章:
http://www.infoq.com/articles/Composite-Programming-Qi4j
我有兴趣了解是否有人正在使用(或已经使用过)Qi4j框架?
与使用传统的依赖注入框架(如Spring)将类连接在一起相比,它是如何比较的.从维护的角度来看,生成的对象图(基于mixins而不是类)更容易处理吗?
我试图像在groovy中那样组织我的构建文件,通过在单独的文件中重用值来重用.但我无法理解在kotlin DSL中做同样事情的语法.
这是我在root build.gradle.kts中使用的内容:
applyFrom("config.gradle.kts")
buildscript {
repositories {
google()
jcenter()
}
dependencies {
val test = project.extra["minSdkVer"]
classpath("com.android.tools.build:gradle:3.0.0-alpha4")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.2-5")
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}
Run Code Online (Sandbox Code Playgroud)
以下是正在引用的config.gradle.kts中的内容:
mapOf(
Pair("minSdkVer", 22),
Pair("targetSdkVer", 25),
Pair("compiledSdkVer", 25),
Pair("buildToolsVer", "26-rc4")
).entries.forEach {
project.extra.set(it.key, it.value)
}
Run Code Online (Sandbox Code Playgroud)
但是有一个错误:
无法在额外的属性扩展上获取属性'minSdkVer',因为它不存在
我正在使用这篇博文来配置Spring Boot项目的集成测试,但我非常坚持声明源集.我也在StackOverflow上发现了这篇文章,但我想我已经进一步了.
我的项目结构是
project
|_ src
|_ main
| |_ kotlin
| |_ resources
|_ testIntegration
| |_ kotlin
| |_ resources
|_ test
| |_ kotlin
| |_ resources
|_ build.gradle.kts
|_ ... other files
Run Code Online (Sandbox Code Playgroud)
和build.gradle.kts
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
idea
kotlin("jvm")
id("org.springframework.boot") version "2.0.5.RELEASE"
id("org.jetbrains.kotlin.plugin.spring") version "1.2.71"
}
fun DependencyHandlerScope.springBoot(module: String) = this.compile("org.springframework.boot:spring-boot-$module:2.0.5.RELEASE")
fun DependencyHandlerScope.springBootStarter(module: String) = this.springBoot("starter-$module")
dependencies {
springBoot("devtools")
springBootStarter("batch")
springBootStarter("... spring boot dependencies")
compile("... more dependencies")
testCompile("... more …Run Code Online (Sandbox Code Playgroud) 我想向包含集成测试的Kotlin项目添加一个额外的“源集”。我已经看到了几篇有关为香草Java项目或Kotlin进行此操作的文章,但使用的是Groovy而不是Kotlin Gradle DSL。
总之,使用Kotlin Gradle DSL:
我希望目录结构看起来像:
src
main
java
kotlin
resources
test
java
kotlin
resources
integration
java
kotlin
resources
Run Code Online (Sandbox Code Playgroud)
有关:
https://ryanharrison.co.uk/2018/07/25/kotlin-add-integration-test-module.html
https://www.petrikainulainen.net/programming/gradle/getting-started-with-gradle-integration-testing/
谢谢
Gradle 有这个新功能,可以监听文件系统事件,而不是接触文件系统来检测本地文件的更改。
可以在命令行上使用 来启用它--watch-fs。如何在我要在源代码管理中检查的文件中持久启用它?
我正在尝试使用以下命令执行我的Kotlin类:
./gradlew -q run < src/main/kotlin/samples/input.txt
Run Code Online (Sandbox Code Playgroud)
这是我的HelloWorld.kt类:
package samples
fun main(args: Array<String>) {
println("Hello, world!")
val lineRead = readLine()
println(lineRead)
}
Run Code Online (Sandbox Code Playgroud)
这是我的build.gradle.kts:
plugins {
kotlin("jvm")
application
}
application {
mainClassName = "samples.HelloWorldKt"
}
dependencies {
compile(kotlin("stdlib"))
}
repositories {
jcenter()
}
Run Code Online (Sandbox Code Playgroud)
代码执行,但不显示input.txt文件中包含的数据.这是我得到的输出:
Hello, world!
null
Run Code Online (Sandbox Code Playgroud)
我希望能够执行上面的gradlew命令,并将input.txt流重定向到stdio.我可以很容易地用C++做到这一点.编译完.cpp文件后,我可以运行:
./my_code < input.txt
Run Code Online (Sandbox Code Playgroud)
并按预期执行.
如何用Kotlin和Gradle实现同样的目标?
更新:基于这个答案,我已经尝试将其添加到build.gradle.kts,但它不是一个有效的语法:
这个问题已经有了答案:如何将所有依赖项包含在一个 jar 文件中,尽管它是针对 Groovy 的
我正在使用 gradlekotlin-dsl并且代码不兼容。我尝试使用几种方法使其工作,包括:
tasks.withType<Jar> {
configurations["compileClasspath"].forEach { file: File ->
copy {
from(zipTree(file.absoluteFile))
}
}
}
Run Code Online (Sandbox Code Playgroud)
虽然这不起作用。那么如何kotlin-dsl在 gradle 中包含依赖呢?
升级到Gradle 4.10时,我在尝试编译时遇到以下错误:
Execution failed for task ':buildSrc:compileKotlin'.
> Could not resolve all files for configuration ':buildSrc:kotlinCompilerPluginClasspath'.
> Could not find org.jetbrains.kotlin:kotlin-scripting-compiler-embeddable:1.2.60.
Searched in the following locations: file:/Users/<user-name>/.gradle/caches/4.10/embedded-kotlin-repo-1.2.60-2/repo/org/jetbrains/kotlin/kotlin-scripting-compiler-embeddable/1.2.60/kotlin-scripting-compiler-embeddable-1.2.60.jar
Required by:
project :buildSrc
> Could not find org.jetbrains.kotlin:kotlin-sam-with-receiver:1.2.60.
Required by:
project :buildSrc
Run Code Online (Sandbox Code Playgroud)
请注意,我正在使用buildSrcGradle编译过程.
构建失败:
thufir@dur:~/NetBeansProjects/kotlin_dsl$
thufir@dur:~/NetBeansProjects/kotlin_dsl$ gradle clean run
> Configure project :
e: /home/thufir/NetBeansProjects/kotlin_dsl/build.gradle.kts:4:12: Unresolved reference: github
FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring root project 'kotlin_dsl'.
> Could not open cache directory 74ykawxta6db3b2bfk9grjikp (/home/thufir/.gradle/caches/4.3.1/gradle-kotlin-dsl/74ykawxta6db3b2bfk9grjikp).
> Internal error: unable to compile script, see log for details
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
* Get more help at https://help.gradle.org
BUILD …Run Code Online (Sandbox Code Playgroud) 我是批处理脚本的新手.我对Batch Scripting知之甚少.
我怀疑的是如何只将一些行从文本文件复制到其他文本文件.
说我的File.txt是
This is sample file.
I want copy this line.
Also this line.
But not this line.
Run Code Online (Sandbox Code Playgroud)
我想复制第2行和第3行,但不使用它们的行号,因为可能会改变.
我到目前为止做的很多:
@ECHO OFF
SET InFile=abc.txt
SET OutFile=Output.txt
IF EXIST "%OutFile%" DEL "%OutFile%"
SET TempFile=Temp.txt
IF EXIST "%TempFile%" DEL "%TempFile%"
IF EXIST "%OutFile%" DEL "%OutFile%"
FOR /F "tokens=*" %%A IN ('FINDSTR "I want" "%InFile%"') DO (
ECHO.%%A> "%TempFile%"
ECHO.%TempFile%>>"%OutFile%"
REM CALL :RemovePrecedingWordA "%%A"
)
FOR /F "tokens=*" %%A IN ('FINDSTR " Also this" "%InFile%"') DO (
ECHO.%%A> "%TempFile%"
ECHO.%TempFile%>>"%OutFile%"
REM …Run Code Online (Sandbox Code Playgroud)