21

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 ~~

Ace.Yin
  • 845
  • 2
  • 9
  • 19

2 Answers2

23

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>
Ilya
  • 21,871
  • 8
  • 73
  • 92
  • @llya , thanks, you are right. after add the and the maven-source-plugin generated the source.jar . – Ace.Yin Nov 24 '16 at 01:03
  • 4
    Unfortunately if your project has both Java and Kotlin source roots, doing this will cause your sources artifact to only contain Kotlin sources. – Alex Mar 12 '17 at 14:19
22

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>
Alex
  • 2,435
  • 17
  • 18
  • 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. thanks – Ace.Yin Mar 14 '17 at 06:24
  • The `kotlin-maven-plugin` plugin might be another option, check this: https://stackoverflow.com/questions/29330311 – Eric Feb 11 '20 at 12:09