0

I'm working on an Android project and have some core code(that has some dependencies) that i'd like to version and make into a library/artifact(?) that I can pull into other projects. I'm using Maven to build and my editor is IntelliJ. I've never created a .jar but I think that's what I want to do.

In IntelliJ, i've gone to File->Project Structure->Artifacts->+ but I'm lost. I don't know how to define which source directories and files to include in the jar and I'm unsure if I need to include the actual jars of its dependencies in the jar? or define those dependencies in a pom.xml file and include the pom.xml file in the jar?

Any clarification would be much appreciated.

Lain
  • 2,166
  • 4
  • 23
  • 47
David
  • 10,418
  • 17
  • 72
  • 122

2 Answers2

1

Maven project's dependencies are defined in its pom.xml file (basically, changing the dependencies from here is what are you visually doing using Intellij, by File->Project Structure->Artifacts). The source files that Maven takes are the ones which are into the src/main directory of your project. The build is done by default excluding the dependencies, so if you want to include them in your final jar, you have to specify this in your pom file (plugins):

  <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>attached</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
    </configuration>
  </plugin>

After that you have to execute mvn install command to have your jar created. You can do it running maven externally or from your IDE (if you have some Maven plugin installed) and the jar will be created in project's target folder.

Community
  • 1
  • 1
Aritz
  • 30,971
  • 16
  • 136
  • 217
  • can you make Maven only look at certain directories/files in the src/main directory? The files i want in the jar are contained in an Android application. I don't want to the entire Android app in the jar, just certain files. – David Jul 16 '13 at 13:39
  • I suppose you're maintaining the layout of an Android project. Why should you put only certain files? It's supposed everything you put in that directory to be useful. If not, it's not properly located. – Aritz Jul 16 '13 at 13:46
  • The configuration can be modified to [include/exclude a certain package structure](http://maven.apache.org/plugins/maven-jxr-plugin/examples/include-exclude.html) eg. ... **/include/*.java ... – Levity Jul 16 '13 at 13:46
  • @XtremeBiker so the proper way to develop a library for android is to package the whole android app up into a jar? that doesn't seem right – David Jul 16 '13 at 14:01
  • That's the proper way to create a jar with maven, as far as I know in android you prepare an apk, not a jar.. – Aritz Jul 16 '13 at 14:54
  • By the way, in my opinion you should change the question title to point to the subject in a more related way (the title itself says nothing about android) – Aritz Jul 16 '13 at 20:50
0

The settings in IntelliJ IDEA will only create the JAR when you build using the IntelliJ IDEA compiler. To have maven create the JAR and deploy it to the maven repository, you need to use the maven assembly plugin. There is a predefined configuration for creating a jar with dependencies.

The Maven Shade Plugin can also create JARs and handle more advanced use cases. But you'll probably want to start with the assembly plugin/

Javaru
  • 30,412
  • 11
  • 93
  • 70