简体   繁体   English

使用ant脚本编译库

[英]Compiling library with ant script

I created package (project) Point , which contains classes Square , Rectangle , Point , Circle and Line . 我创建了Point包(项目),其中包含SquareRectanglePointCircleLine They are simple classes with constructors for creating said objects. 它们是简单的类,带有用于创建所述对象的构造函数。 From main in Point, you call them like this: 在Point in main中,您可以这样称呼他们:

Point p1 = new Point(0,3);

I should write a program, that asks user to select which object he wants to create and set its geometry and i can only use my Point package as a library. 我应该编写一个程序,要求用户选择要创建的对象并设置其几何形状,并且我只能将Point包用作库。

1) create the simple-graphics.jar library. 1)创建simple-graphics.jar库。 I removed main method in my Point package and managed to produce simple-graphics.jar executable .jar file from my Point package. 我从Point包中删除了main方法,并设法从Point包中生成了simple-graphics.jar可执行文件.jar文件。

2) I am asked to create some ant script that compiles this library from its source files and generates .jar file, however, i have no clue how to do that and if i havent already done it in 1), tutorials on ant scripts are not very clear to me. 2)我被要求创建一些蚂蚁脚本,从其源文件编译该库并生成.jar文件,但是,我不知道该怎么做,如果我在1)中还没有做过,关于蚂蚁脚本的教程是我不太清楚。 I guess im supposed to do it both ways, by selecting produce .jar option in NetBeans and by having somewhere this ant script. 我猜我应该通过在NetBeans中选择Produce .jar选项并在某处放置此ant脚本来实现这两种方式。

3) I should be able to run .jar file generated by 2) by using java -jar simple-graphics.jar How do i do that in NetBeans, or should i use cmd? 3)我应该能够通过使用java -jar simple-graphics.jar运行2)生成的.jar文件,如何在NetBeans中做到这一点,还是应该使用cmd? Im on W7. 我在W7。

EDIT: Thank you for the script, as a look at it i definitely wouldnt be able to write all of that. 编辑:谢谢您的脚本,作为一种看,我肯定无法编写所有脚本。

How do i use this library in my program? 如何在程序中使用此库? SOLVED - like this: 解决-像这样:

package simpleapp;

import point;

public class SimpleApp{



  public static void main(String [] args){


     //Please press 1 to create Point
     //Please specify x and y axis:
     //i will select the type of object and create it
     //Object o = new whichObject(1)(x,y); 

  }
}

Point class in package Point, other classes are very similar: 包Point中的Point类,其他类非常相似:

package point;

public class Point{

 double x;
 double y;

 public Point(double a, double b){
  x = a;
  y = b;
 }

 public Point(){
  x = 0;
  y = 0;
 }

 public double distance(Point p){
   return Math.sqrt((p.x - x) * (p.x - x) + (p.y - y) * (p.y-y));
 }
}

Here is a very basic Ant script ( build.xml ) to compile your Java files to class files and pack them into a JAR file assuming your Java source files are in subdirectory src . 这是一个非常基本的Ant脚本( build.xml ),用于将Java文件编译为类文件,并将它们打包到JAR文件中,前提是您的Java源文件位于子目录src Be aware that this is only a starting point. 请注意,这只是一个起点。

<project name="Point-Library" default="build">

  <property name="src.dir" value="src" />
  <property name="build.dir" value="build" />
  <property name="jar.name" value="simple-graphics.jar" />

  <target name="build" depends="prepare, compile, jar" />

  <target name="prepare" description="Creates the build folder">
    <mkdir dir="${build.dir}" />
  </target>

  <target name="compile" description="Compiles the Java source files">
    <javac srcdir="${src.dir}" destdir="${build.dir}" />
  </target>

  <target name="jar" description="Packs the compiled Java classes into a JAR file">
    <jar basedir="${build.dir}" destfile="${jar.name}" />
  </target>

</project>

But you should first read the offical Ant manual to understand the concepts. 但是,您首先应该阅读Ant官方手册以了解这些概念。

To use the resulting JAR file you have to add it to your classpath (which you did already). 要使用生成的JAR文件,您必须将其添加到您的类路径中(您已经这样做了)。 Then you can import the package point . 然后,您可以导入打包point

package simpleapp;

import point; // <---

public class SimpleApp{



  public static void main(String [] args){


     //Please press 1 to create Point
     //Please specify x and y axis:
     //i will select the type of object and create it
     //Object o = new whichObject(1)(x,y); 

  }
}

It makes no difference for your client code which uses the point classes whether their source code is part of your project or you import only the JAR file containing the point class files. 对于使用point类的客户端代码,无论其源代码是项目的一部分,还是仅导入包含point类文件的JAR文件,这都没有区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM