All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.adamcin.snagjar.Snaggable.scala Maven / Gradle / Ivy

Go to download

Scan a directory for java libraries identifiable as maven artifacts and perform list, install, deploy, and other operations on them.

There is a newer version: 1.3.4
Show newest version
/*
 * This is free and unencumbered software released into the public domain.
 *
 * Anyone is free to copy, modify, publish, use, compile, sell, or
 * distribute this software, either in source code form or as a compiled
 * binary, for any purpose, commercial or non-commercial, and by any
 * means.
 *
 * In jurisdictions that recognize copyright laws, the author or authors
 * of this software dedicate any and all copyright interest in the
 * software to the public domain. We make this dedication for the benefit
 * of the public at large and to the detriment of our heirs and
 * successors. We intend this dedication to be an overt act of
 * relinquishment in perpetuity of all present and future rights to this
 * software under copyright law.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * For more information, please refer to 
 */

package net.adamcin.snagjar

import java.io.{StringWriter, File}

import org.apache.maven.model.Model
import org.apache.maven.model.io.xpp3.MavenXpp3Writer

import scalax.io.Resource

/**
 * Simple class representing a discovered jar, its maven coordinates, and its pom file
 * @param session the current SnagSession
 * @param gav the artifact's maven coordinates
 * @param jar the artifact's jar file
 * @param pom the artifact's pom file
 * @since 0.8.0
 * @author Mark Adamcin
 */
class Snaggable(val session: SnagSession,
                val gav: GAV,
                val jar: File,
                val pom: File) {



  def toGenerated(): Snaggable = {
    val genGav = gav.toSnapshot()
    new Snaggable(session, genGav, jar, Snaggable.genPom(session, genGav))
  }
}

/**
 * Companion class defining a convenient apply method
 */
object Snaggable {

  /**
   * Extract the provided file with the provided SnagSession
   * @param file jar file to extract
   * @param session SnagSession to extract the file with
   * @return an extracted Snaggable
   */
  def apply(file: File, session: SnagSession): Snaggable = SnagSession.extract(file, session)

  /**
   * Generates the stub pom file if it doesn't exist, and returns the file
   * @return a stub pom file
   */
  def genPom(session: SnagSession, gav: GAV): File = {
    val pomGenFile = session.createTempFile
    val model = new Model
    model.setModelVersion("4.0.0")
    model.setGroupId(gav.groupId)
    model.setArtifactId(gav.artifactId)
    model.setVersion(gav.version)
    model.setPackaging("jar")
    model.setDescription("artifact pom generated by snagjar-maven-plugin")
    val writer = new StringWriter
    val xppWriter = new MavenXpp3Writer
    xppWriter.write(writer, model)
    writer.close()
    Resource.fromFile(pomGenFile).write(writer.getBuffer.toString)
    pomGenFile
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy