Maven kotlin

Maven kotlin DEFAULT

Maven

Plugin and versions

The kotlin-maven-plugin compiles Kotlin sources and modules. Currently, only Maven v3 is supported.

Define the version of Kotlin you want to use via a kotlin.version property:

<properties> <kotlin.version>1.5.31</kotlin.version> </properties>

Dependencies

Kotlin has an extensive standard library that can be used in your applications. To use the standard library in your project, add the following dependency in the pom file:

<dependencies> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib</artifactId> <version>${kotlin.version}</version> </dependency> </dependencies>

If you're targeting JDK 7 or JDK 8, you can use extended versions of the Kotlin standard library. They contain additional extension functions for APIs added in new JDK versions. Instead of , use or , depending on your JDK version.

For Kotlin versions older that 1.2, use and .

If your project uses Kotlin reflection or testing facilities, you need to add the corresponding dependencies as well. The artifact IDs are for the reflection library, and and for the testing libraries.

Compile Kotlin-only source code

To compile source code, specify the source directories in the tag:

<build> <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory> <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory> </build>

The Kotlin Maven Plugin needs to be referenced to compile the sources:

<build> <plugins> <plugin> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-maven-plugin</artifactId> <version>${kotlin.version}</version> <executions> <execution> <id>compile</id> <goals> <goal>compile</goal> </goals> </execution> <execution> <id>test-compile</id> <goals> <goal>test-compile</goal> </goals> </execution> </executions> </plugin> </plugins> </build>

Compile Kotlin and Java sources

To compile projects that include Kotlin and Java source code, invoke the Kotlin compiler before the Java compiler. In maven terms that means that should be run before using the following method, making sure that the plugin comes before the in your file:

<build> <plugins> <plugin> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-maven-plugin</artifactId> <version>${kotlin.version}</version> <executions> <execution> <id>compile</id> <goals> <goal>compile</goal> </goals> <configuration> <sourceDirs> <sourceDir>${project.basedir}/src/main/kotlin</sourceDir> <sourceDir>${project.basedir}/src/main/java</sourceDir> </sourceDirs> </configuration> </execution> <execution> <id>test-compile</id> <goals> <goal>test-compile</goal> </goals> <configuration> <sourceDirs> <sourceDir>${project.basedir}/src/test/kotlin</sourceDir> <sourceDir>${project.basedir}/src/test/java</sourceDir> </sourceDirs> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <executions> <!-- Replacing default-compile as it is treated specially by maven --> <execution> <id>default-compile</id> <phase>none</phase> </execution> <!-- Replacing default-testCompile as it is treated specially by maven --> <execution> <id>default-testCompile</id> <phase>none</phase> </execution> <execution> <id>java-compile</id> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> <execution> <id>java-test-compile</id> <phase>test-compile</phase> <goals> <goal>testCompile</goal> </goals> </execution> </executions> </plugin> </plugins> </build>

Incremental compilation

To make your builds faster, you can enable incremental compilation for Maven by defining the property:

<properties> <kotlin.compiler.incremental>true</kotlin.compiler.incremental> </properties>

Alternatively, run your build with the option.

Annotation processing

See the description of Kotlin annotation processing tool ().

Jar file

To create a small Jar file containing just the code from your module, include the following under in your Maven pom.xml file, where is defined as a property and points to the main Kotlin or Java class:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.6</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <mainClass>${main.class}</mainClass> </manifest> </archive> </configuration> </plugin>

Self-contained Jar file

To create a self-contained Jar file containing the code from your module along with dependencies, include the following under in your Maven pom.xml file, where is defined as a property and points to the main Kotlin or Java class:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.6</version> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <archive> <manifest> <mainClass>${main.class}</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </execution> </executions> </plugin>

This self-contained jar file can be passed directly to a JRE to run your application:

java -jar target/mymodule-0.0.1-SNAPSHOT-jar-with-dependencies.jar

Specifying compiler options

Additional options and arguments for the compiler can be specified as tags under the element of the Maven plugin node:

<plugin> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-maven-plugin</artifactId> <version>${kotlin.version}</version> <executions>...</executions> <configuration> <nowarn>true</nowarn> <!-- Disable warnings --> <args> <arg>-Xjsr305=strict</arg> <!-- Enable strict mode for JSR-305 annotations --> ... </args> </configuration> </plugin>

Many of the options can also be configured through properties:

<project ...> <properties> <kotlin.compiler.languageVersion>1.0</kotlin.compiler.languageVersion> </properties> </project>

The following attributes are supported:

Attributes common to JVM and JS

NameProperty nameDescriptionPossible valuesDefault value
Generate no warningstrue, falsefalse
kotlin.compiler.languageVersionProvide source compatibility with the specified version of Kotlin"1.2" (DEPRECATED), "1.3", "1.4", "1.5" (EXPERIMENTAL)
kotlin.compiler.apiVersionAllow using declarations only from the specified version of bundled libraries"1.2" (DEPRECATED), "1.3", "1.4", "1.5" (EXPERIMENTAL)
The directories containing the source files to compileThe project source roots
Enabled compiler plugins[]
Options for compiler plugins[]
Additional compiler arguments[]

Attributes specific to JVM

NameProperty nameDescriptionPossible valuesDefault value
Target version of the generated JVM bytecode"1.6" (DEPRECATED), "1.8", "9", "10", "11", "12", "13", "14", "15", "16""1.8"
Include a custom JDK from the specified location into the classpath instead of the default JAVA_HOME

Attributes specific to JS

NameProperty nameDescriptionPossible valuesDefault value
Destination *.js file for the compilation result
Generate .meta.js and .kjsm files with metadata. Use to create a librarytrue, falsetrue
Generate source maptrue, falsefalse
Embed source files into source map"never", "always", "inlining""inlining"
Add the specified prefix to paths in the source map
The kind of JS module generated by the compiler"umd", "commonjs", "amd", "plain""umd"

Generating documentation

The standard JavaDoc generation plugin () does not support Kotlin code. To generate documentation for Kotlin projects, use Dokka; please refer to the Dokka README for configuration instructions. Dokka supports mixed-language projects and can generate output in multiple formats, including standard JavaDoc.

OSGi

For OSGi support see the Kotlin OSGi page.

Last modified: 02 September 2021

GradleAnt

Sours: https://kotlinlang.org/docs/maven.html

Maven for Java and Kotlin

Prerequisites

  • You have a project written in Java or Kotlin.

  • Your project uses Maven.

  • If you want to publish artifacts to Space Packages, make sure the project has a Maven repository.

Eligible images

The typical task when working with a Maven project is to build the project and run tests, and publish Maven artifacts after that. Space Automation doesn't provide any helper functions for Maven, therefore your Automation scripts must directly use the tool.

Building the project and running tests

To build a project and run tests, you should run the command. This is how you can do this in :

job("Build and run tests") { container(displayName = "Run mvn install", image = "maven:latest") { shellScript { content = """ mvn clean install """ } } }

Publishing Maven artifacts to Space Packages

To publish artifacts of a Maven project to a Packages repository, you should configure artifact properties, reference a repository, and specify authentication credentials. This configuration is performed in the file. The problem is that for security reasons, user credentials for a Maven repository must be stored not in the project directory but in the user's directory in a separate file. The solution is to put to the project directory and provide user credentials through environment variables.

  1. Make sure your project has a Maven repository.

  2. Configure repository settings in the project's . Typically, all you should do is add the node with the Space Packages repository parameters:

    <!-- other content --> <distributionManagement> <repository> <id>space-maven</id> <!-- provide url via the 'repositoryUrl' command-line arg --> <url>${repositoryUrl}</url> </repository> </distributionManagement> <!-- other content -->

  3. Create in the project root. The file should contain credentials to the Packages repository which we will provide as command-line arguments:

    <!-- other content --> <settings> <servers> <server> <id>space-maven</id> <!-- provide credentials via the command-line args: --> <!-- 'spaceUsername' and 'spacePassword' --> <username>${spaceUsername}</username> <password>${spacePassword}</password> </server> </servers> </settings>

  4. In the project root, create the file with the build script:

    job("Build, run tests, publish") { container(displayName = "Run publish script", image = "maven:3-openjdk-8-slim") { // url of a Space Packages repository env["REPOSITORY_URL"] = "https://maven.pkg.jetbrains.space/mycompany/p/key/my-maven-repo" shellScript { content = """ echo Build and run tests... mvn clean install echo Publish artifacts... mvn versions:set -DnewVersion=1.0.${'$'}JB_SPACE_EXECUTION_NUMBER mvn deploy -s settings.xml \ -DrepositoryUrl=${'$'}REPOSITORY_URL \ -DspaceUsername=${'$'}JB_SPACE_CLIENT_ID \ -DspacePassword=${'$'}JB_SPACE_CLIENT_SECRET """ } } }

    Here we:

    • Provide the repository URL through the environment variable.

    • Specify that are stored in the project root directory.

    • Use the variable to specify the artifact version as .

    • Provide Space Automation credentials through the and environment variables.

Publishing Maven artifacts to external repositories

Publishing artifacts to external Maven repositories is almost the same as publishing to a Space Packages repository. The only difference is that you should store credentials to the repository in the Secrets&Parameters storage.

  1. Repeat steps 1 and 2 from Publishing Maven artifacts to Space Packages.

  2. Create a parameter and a secret for storing the username and password that the script must use to access the external repository.

    Secrets and parameters
  3. Edit the file:

    job("Build, run tests, publish") { container(displayName = "Run publish script", image = "maven:3-openjdk-8-slim") { env["REPOSITORY_URL"] = "https://externalrepo.example.com" env["USERNAME"] = Params("repo_user") env["PASSWORD"] = Secrets("repo_password") shellScript { content = """ echo Build and run tests... mvn clean install echo Publish artifacts... mvn versions:set -DnewVersion=1.0.${'$'}JB_SPACE_EXECUTION_NUMBER mvn deploy -s settings.xml \ -DrepositoryUrl=${'$'}REPOSITORY_URL \ -DspaceUsername=${'$'}USERNAME \ -DspacePassword=${'$'}PASSWORD """ } } }

Last modified: 18 August 2021

Gradle for Java and Kotlin.NET Core

Sours: https://www.jetbrains.com/help/space/maven-with-java-and-kotlin.html
  1. 2010 hp desktop
  2. Rustic curtain hooks
  3. Toyota c60 transmission

Using Maven

Edit Page

Plugin and Versions

The kotlin-maven-plugin compiles Kotlin sources and modules. Currently only Maven v3 is supported.

Define the version of Kotlin you want to use via kotlin.version. The correspondence between Kotlin releases and versions is displayed below:

MilestoneVersion
1.0.5-21.0.5-2
1.0.51.0.5
1.0.41.0.4
1.0.31.0.3
1.0.2 hotfix update1.0.2-1
1.0.21.0.2
1.0.1 hotfix update 21.0.1-2
1.0.1 hotfix update1.0.1-1
1.0.11.0.1
1.0 GA1.0.0
Release Candidate1.0.0-rc-1036
Beta 41.0.0-beta-4589
Beta 31.0.0-beta-3595
Beta 21.0.0-beta-2423
Beta1.0.0-beta-1103
Beta Candidate1.0.0-beta-1038
M140.14.449
M130.13.1514
M12.10.12.613
M120.12.200
M11.10.11.91.1
M110.11.91
M10.10.10.195
M100.10.4
M90.9.66
M80.8.11
M70.7.270
M6.20.6.1673
M6.10.6.602
M60.6.69
M5.30.5.998

Dependencies

Kotlin has an extensive standard library that can be used in your applications. Configure the following dependency in the pom file

Compiling Kotlin only source code

To compile source code, specify the source directories in the

The Kotlin Maven Plugin needs to be referenced to compile the sources:

Compiling Kotlin and Java sources

To compile mixed code applications Kotlin compiler should be invoked before Java compiler. In maven terms that means kotlin-maven-plugin should be run before maven-compiler-plugin.

It could be done by moving Kotlin compilation to previous phase, process-sources (feel free to suggest a better solution if you have one):

OSGi

For OSGi support see the Kotlin OSGi page.

Examples

An example Maven project can be downloaded directly from the GitHub repository

Sours: https://dogwood008.github.io/kotlin-web-site-ja/docs/reference/using-maven.html
Kotlin Course - Tutorial for Beginners

.

Kotlin maven

.

run tests in parallel using maven surefire plugin

.

Similar news:

.



498 499 500 501 502