maven-source-plugin not work for kotlin
Asked Answered
C

2

22

I am trying to use maven-source-plugin to create a source.jar for my kotlin project, but seems the maven-source-plugin not work well for kotlin project.

when i run "mvn source:jar", the output message always says:

[INFO] No sources in project. Archive not created.

here is the maven-source-plugin configuration in my pom file of the project:

    <build>
    <plugins>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>2.2.1</version>
            <executions>
                <execution>
                    <id>attach-sources</id>
                    <phase>package</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                    <configuration>
                        <attach>true</attach>
                        <includes>
         <!-- i am trying to specify the include dir manually, but not work -->                               
                         <include>${project.basedir}/src/main/kotlin/*</include>
                        </includes>
                        <forceCreation>true</forceCreation>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

my question is: how to attach kotlin source files using maven-source-plugin?

thanks ~~

Calamity answered 23/11, 2016 at 12:3 Comment(0)
T
24

By default maven expects sources to be in src/main/java directory. If you use non-default directories, you have to specify them in build element:

<build>
    <sourceDirectory>src/main/kotlin</sourceDirectory>
    <testSourceDirectory>src/test/kotlin</testSourceDirectory>
</build>
Tintometer answered 23/11, 2016 at 20:2 Comment(2)
@llya , thanks, you are right. after add the <sourceDirectory> and <testSourceDirectory> the maven-source-plugin generated the source.jar .Calamity
Unfortunately if your project has both Java and Kotlin source roots, doing this will cause your sources artifact to only contain Kotlin sources.Sphingosine
S
24

When your project has mixed Java and Kotlin (i.e. multiple source roots), I found that using the build-helper-maven-plugin worked well to ensure that both Java and Kotlin sources are included in the built sources artifact.

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <version>3.0.0</version>

  <executions>
    <execution>
      <phase>generate-sources</phase>
      <goals>
        <goal>add-source</goal>
      </goals>
      <configuration>
        <sources>
          <source>src/main/kotlin</source>
        </sources>
      </configuration>
    </execution>
  </executions>
</plugin>
Sphingosine answered 12/3, 2017 at 14:19 Comment(2)
thanks Alex, there are only kotlin codes in my projects. i will try your suggestion when there are both java and kotlin files in the project. thanksCalamity
The kotlin-maven-plugin plugin might be another option, check this: stackoverflow.com/questions/29330311Piccadilly

© 2022 - 2024 — McMap. All rights reserved.