如何管理GitHub上的外部JS/CSS库

时间:2022-10-19 23:24:17

For my Java project which uses Maven I have added Twitters Bootstrap lately to my repository on GitHub. Now I have noticed that my GitHub language statistic changed from

对于使用Maven的Java项目,我最近在GitHub上添加了Twitters Bootstrap。现在我注意到我的GitHub语言统计数据已经发生了变化

  • 100% Java
  • 100%的Java

to

  • 66,% Java 33,3% JavaScript
  • 66年,33岁的% Java 3% JavaScript

Since I didn't write the JS part, is there a way to manage that this isn't counted as mine or how would one normally manage foreign JS code in a GitHub repository or projects in general

由于我没有编写JS部分,是否有一种方法可以管理它不被算作我的,或者如何在GitHub存储库或项目中管理外国JS代码

5 个解决方案

#1


14  

I have faced that problem lately, too. There are several ways to deal with it:

我最近也遇到了这个问题。有几种方法可以解决这个问题:

  • use the performance optimized versions, for instance bootstrap.min.js instead of bootstrap.js etc. GitHub uses the file size to determine the language proportion of each repository. This won't reduce it to zero, but at least it reduces the proportion

    使用性能优化版本,例如bootstrap.min。js而不是引导。GitHub使用文件大小来确定每个存储库的语言比例。这不会把它减少到零,但至少它减少了比例

  • This might be what you want: GitHub has a vendor list with regular expressions which are exluced from the language graph. There you will see, that jQuery is already part of the exclusion. There is also a pending pull request to add Twitter Bootstrap. If this does not get pass you can always add the files to a directory called vendor there they will be excluded from the statistics.

    这可能是您想要的:GitHub有一个带有正则表达式的供应商列表,这些正则表达式可以从语言图中显示出来。在那里,您将看到,jQuery已经被排除在外了。另外还有一个添加Twitter引导程序的拖拽请求。如果没有通过,您可以将文件添加到一个名为vendor的目录中,它们将被排除在统计数据之外。

  • use the Maven AntRun Plugin: With it you could simply download the JavaScript library files on request with Ant Get

    使用Maven AntRun插件:使用它,您可以简单地下载JavaScript库文件,请求Ant获得。

  • there are also Maven repositories for these JavaScript libraries: Bootstrap-Maven. However, the files are mounted at /ext/bootstrap/ and I have no idea how this helps

    这些JavaScript库也有Maven存储库:Bootstrap-Maven。但是,这些文件安装在/ext/bootstrap/上,我不知道这有什么用

  • there are even more solutions, like Maven War Plugin using overlays.

    还有更多的解决方案,比如使用覆盖的Maven War插件。

Finally my advice: just put the files into your repository. I have looked into some bigger projects on GitHub using jQuery and Twitter Bootstrap and they all put their JavaScript library files into the repository.

最后,我的建议是:将文件放入您的存储库中。我研究了一些使用jQuery和Twitter引导的GitHub上更大的项目,它们都将自己的JavaScript库文件放入了库中。

The advantage of these JavaScript library files is that they don't have to be compiled what so ever. Use this advantage and don't add additional burdens for the sake of aesthetic. Best practice seems to be adding it to the repository and live with the stupid language graph. Who knows, maybe certain files will be ignored in the future.

这些JavaScript库文件的优点是,它们不需要编译什么。利用这个优势,不要为了美观而增加额外的负担。最佳实践似乎是将它添加到存储库中,并使用愚蠢的语言图。谁知道呢,也许将来某些文件会被忽略。

#2


3  

Two observations:

两个观察:

  • The GitHub statistics are not incorrect: your project consists of those amounts of Java and JavaScript code. It would be incorrect to say that it is a 100% Java project.
  • GitHub的统计数据并不是不正确的:您的项目包含了大量的Java和JavaScript代码。说它是一个100%的Java项目是错误的。
  • It's not common to include dependencies in your own code repository. You install them manually or automatically (using a system/method that varies for each programming language / environment). For example, Python has a file called requirements.txt which lists the dependencies but in Java I can imagine there to be a similar approach.
  • 在自己的代码库中包含依赖项并不常见。您可以手动或自动地安装它们(使用不同编程语言/环境的系统/方法)。例如,Python有一个名为requirements的文件。txt列出了依赖项,但在Java中我可以想象有类似的方法。

#3


3  

Use git submodules or put your 3rd party files in a directory called 'vendor'. Then Github will not count them as part of your code

使用git子模块或将第三方文件放在一个名为“vendor”的目录中。然后Github不会将它们算作代码的一部分

#4


1  

It is a Maven best practice to split a project into several submodules, each one with a small, well-defined purpose. If you're putting different kinds of sources in the same Maven module, you're not using it right. Assuming that in the end you'll end up with a .war, then my advice is to add Bootstrap, jQuery and whatever other libraries you're using as zip dependencies that will be included in the war build.

将一个项目分成几个子模块是Maven的最佳实践,每个子模块都有一个小的、定义良好的用途。如果在同一个Maven模块中放置不同类型的源,那么您没有正确地使用它。假设最终您将得到一个.war,那么我的建议是添加Bootstrap、jQuery和任何您正在使用的作为war构建中包含的zip依赖项的其他库。

Let's assume that you can get these libraries available as downloadable Maven dependencies (I'll discuss below your options for accomplishing this), then you would just have to declare them as dependencies for your war artifact:

让我们假设您可以将这些库作为可下载的Maven依赖项(我将在下面讨论您的选择来实现这一点),然后您只需将它们声明为您的war工件的依赖项:

  <dependency>
    <groupId>com.twitter.bootstrap</groupId>
    <artifactId>twitter-bootstrap</artifactId>
    <version>2.0.4</version>
    <type>zip</type>
    <scope>runtime</scope>
  </dependency>

As far as your own code is concerned, that is all the Bootstrap that you need to "include".

就您自己的代码而言,这就是您需要“包含”的所有引导程序。

How do you get that zip available? Well, the best thing would be to see it published on the central Maven repository, but that's not something that you should do; Twitter should step up and take care of all the Maven users out there.

你怎么得到那个zip文件?最好的办法是在*Maven存储库上发布,但这不是你应该做的;Twitter应该加强并照顾所有的Maven用户。

The next best thing would be to find another public repository that already hosts it well packaged, but I couldn't find one with a nice packaging after a short Google search, maybe you'll have more luck.

下一个最好的方法是找到另一个公共存储库,它已经被很好地打包了,但是在简短的谷歌搜索之后,我找不到一个具有很好的打包,也许您会更幸运。

The third option is to take things into your own hands, and be the one that creates the proper Maven build for that zip and publish it on your own repository.

第三种选择是将事情掌握在自己的手中,并且是创建适当的Maven构建并将其发布到自己的存储库中的工具。

One solution I prefer for building such resource zips is to use the maven assembly plugin, and you can use as an inspiration this maven project: put the sources (js, css and image files) in src/main/resources, and add a simple assembly file that just copies all the files from the resources directory into a zip. Set up a maven repository on a server (a simple HTTP serving machine with sftp access is enough), and do a maven release on that project to upload the artifact on the server.

为建立这样一个解决方案相比,我更喜欢资源拉链是使用maven插件,组装,您可以使用作为一个灵感这maven项目:把来源(js、css和图像文件)在src /主/资源,并添加一个简单的汇编文件,从资源目录的所有文件复制到一个zip。在服务器上设置一个maven存储库(使用sftp访问的简单HTTP服务器就足够了),并在该项目上执行maven版本,以便将工件上传到服务器上。

(You should have a maven repository for releasing your own projects anyway)

(不管怎样,您应该有一个maven存储库来释放您自己的项目)

Next, you would need to declare your repository in your real project's pom file, add a dependency on the bootstrap artifact, and configure the maven-dependency-plugin to unzip it in a location of your choice:

接下来,您需要在真实项目的pom文件中声明您的存储库,添加对引导工件的依赖项,并配置maven- dependent -plugin,在您选择的位置解压缩它:

...
  <repositories><repository>
    <id>bootstrap-repo</id>
    <name>bootstrap public repository</name>
    <url>http://repo.yourhost.net/release</url>
    <releases>
      <enabled>true</enabled>
    </releases>
    <snapshots>
      <enabled>false</enabled>
    </snapshots>
  </repository></repositories>
...
  <dependencies><dependency>
    <groupId>com.twitter.bootstrap</groupId>
    <artifactId>twitter-bootstrap</artifactId>
    <version>2.0.4</version>
    <type>zip</type>
    <scope>runtime</scope>
  </dependency></dependencies>
...
  <build><plugins><plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
      <execution>
        <id>unpack-dependencies</id>
        <phase>prepare-package</phase>
        <goals>
          <goal>unpack</goal>
        </goals>
        <configuration>
          <artifactItems>
            <!-- Unpack the Bootstrap library -->
            <artifactItem>
              <groupId>com.twitter</groupId>
              <artifactId>twitter-bootstrap</artifactId>
              <type>zip</type>
              <outputDirectory>${project.build.directory}/bootstrap</outputDirectory>
            </artifactItem>
          </artifactItems>
        </configuration>
      </execution>
    </executions>
  </plugin></plugins></build>
...

(I'm not sure it would be safe to use com.twitter as the groupId, since they're not the ones publishing that artifact, so use your own groupId)

)我不敢肯定使用com是否安全。twitter作为一个群体,因为他们不是发布那个工件的人,所以使用你自己的群体)

Since your own code just declares a dependency on this twitter-bootstrap artifact, you can keep your main GitHub repository clean of third party code. You don't have to commit the project that builds the twitter-bootstrap artifact at all if you don't want your coding statistics to be influenced, although every line of code you write should be committed.

由于您自己的代码只是声明了对这个twitter-bootstrap工件的依赖,您可以保持您的主GitHub存储库清除第三方代码。如果您不希望您的编码统计数据受到影响,那么您完全不需要提交构建twitter-bootstrap工件的项目,尽管您编写的每一行代码都应该提交。

Alternative: maven-antrun-plugin

An alternative to using a mavenized packaging of third party libraries is to extract those files from their official zip distributions using ant tasks in the maven-antrun-plugin to download the zip and extract the files from it.

使用mavenized打包第三方库的另一种方法是使用maven-antrun-plugin中的ant任务从它们的官方zip分发包中提取这些文件,以下载zip并从中提取文件。

Or, you can use this trick in the twitter-bootstrap maven project in order to avoid copying their source files at all.

或者,您可以在twitter-bootstrap maven项目中使用这个技巧,以避免复制它们的源文件。

#5


1  

The question is fairly old, but for future reference, it should be noted that GitHub added in 2014 a new feature to handle this thing. Possibly thanks to an open issue on the linguist repository.

这个问题相当古老,但是为了以后的参考,应该注意GitHub在2014年添加了一个新特性来处理这个问题。可能要感谢语言学家知识库上的一个开放问题。

This feature allows regarding certain files as libraries, documentation or a specific language.

此特性允许将某些文件视为库、文档或特定语言。

Just add .gitattributes file to your repository. For example, to ignore a library folder, just add the line:

只需将.gitattributes文件添加到存储库中。例如,要忽略一个库文件夹,只需添加一行:

library/* linguist-vendored

Or to set the docs folder, add the line:

或者设置docs文件夹,添加行:

docs/* linguist-documentation

More examples can be found here.

更多的例子可以在这里找到。

#1


14  

I have faced that problem lately, too. There are several ways to deal with it:

我最近也遇到了这个问题。有几种方法可以解决这个问题:

  • use the performance optimized versions, for instance bootstrap.min.js instead of bootstrap.js etc. GitHub uses the file size to determine the language proportion of each repository. This won't reduce it to zero, but at least it reduces the proportion

    使用性能优化版本,例如bootstrap.min。js而不是引导。GitHub使用文件大小来确定每个存储库的语言比例。这不会把它减少到零,但至少它减少了比例

  • This might be what you want: GitHub has a vendor list with regular expressions which are exluced from the language graph. There you will see, that jQuery is already part of the exclusion. There is also a pending pull request to add Twitter Bootstrap. If this does not get pass you can always add the files to a directory called vendor there they will be excluded from the statistics.

    这可能是您想要的:GitHub有一个带有正则表达式的供应商列表,这些正则表达式可以从语言图中显示出来。在那里,您将看到,jQuery已经被排除在外了。另外还有一个添加Twitter引导程序的拖拽请求。如果没有通过,您可以将文件添加到一个名为vendor的目录中,它们将被排除在统计数据之外。

  • use the Maven AntRun Plugin: With it you could simply download the JavaScript library files on request with Ant Get

    使用Maven AntRun插件:使用它,您可以简单地下载JavaScript库文件,请求Ant获得。

  • there are also Maven repositories for these JavaScript libraries: Bootstrap-Maven. However, the files are mounted at /ext/bootstrap/ and I have no idea how this helps

    这些JavaScript库也有Maven存储库:Bootstrap-Maven。但是,这些文件安装在/ext/bootstrap/上,我不知道这有什么用

  • there are even more solutions, like Maven War Plugin using overlays.

    还有更多的解决方案,比如使用覆盖的Maven War插件。

Finally my advice: just put the files into your repository. I have looked into some bigger projects on GitHub using jQuery and Twitter Bootstrap and they all put their JavaScript library files into the repository.

最后,我的建议是:将文件放入您的存储库中。我研究了一些使用jQuery和Twitter引导的GitHub上更大的项目,它们都将自己的JavaScript库文件放入了库中。

The advantage of these JavaScript library files is that they don't have to be compiled what so ever. Use this advantage and don't add additional burdens for the sake of aesthetic. Best practice seems to be adding it to the repository and live with the stupid language graph. Who knows, maybe certain files will be ignored in the future.

这些JavaScript库文件的优点是,它们不需要编译什么。利用这个优势,不要为了美观而增加额外的负担。最佳实践似乎是将它添加到存储库中,并使用愚蠢的语言图。谁知道呢,也许将来某些文件会被忽略。

#2


3  

Two observations:

两个观察:

  • The GitHub statistics are not incorrect: your project consists of those amounts of Java and JavaScript code. It would be incorrect to say that it is a 100% Java project.
  • GitHub的统计数据并不是不正确的:您的项目包含了大量的Java和JavaScript代码。说它是一个100%的Java项目是错误的。
  • It's not common to include dependencies in your own code repository. You install them manually or automatically (using a system/method that varies for each programming language / environment). For example, Python has a file called requirements.txt which lists the dependencies but in Java I can imagine there to be a similar approach.
  • 在自己的代码库中包含依赖项并不常见。您可以手动或自动地安装它们(使用不同编程语言/环境的系统/方法)。例如,Python有一个名为requirements的文件。txt列出了依赖项,但在Java中我可以想象有类似的方法。

#3


3  

Use git submodules or put your 3rd party files in a directory called 'vendor'. Then Github will not count them as part of your code

使用git子模块或将第三方文件放在一个名为“vendor”的目录中。然后Github不会将它们算作代码的一部分

#4


1  

It is a Maven best practice to split a project into several submodules, each one with a small, well-defined purpose. If you're putting different kinds of sources in the same Maven module, you're not using it right. Assuming that in the end you'll end up with a .war, then my advice is to add Bootstrap, jQuery and whatever other libraries you're using as zip dependencies that will be included in the war build.

将一个项目分成几个子模块是Maven的最佳实践,每个子模块都有一个小的、定义良好的用途。如果在同一个Maven模块中放置不同类型的源,那么您没有正确地使用它。假设最终您将得到一个.war,那么我的建议是添加Bootstrap、jQuery和任何您正在使用的作为war构建中包含的zip依赖项的其他库。

Let's assume that you can get these libraries available as downloadable Maven dependencies (I'll discuss below your options for accomplishing this), then you would just have to declare them as dependencies for your war artifact:

让我们假设您可以将这些库作为可下载的Maven依赖项(我将在下面讨论您的选择来实现这一点),然后您只需将它们声明为您的war工件的依赖项:

  <dependency>
    <groupId>com.twitter.bootstrap</groupId>
    <artifactId>twitter-bootstrap</artifactId>
    <version>2.0.4</version>
    <type>zip</type>
    <scope>runtime</scope>
  </dependency>

As far as your own code is concerned, that is all the Bootstrap that you need to "include".

就您自己的代码而言,这就是您需要“包含”的所有引导程序。

How do you get that zip available? Well, the best thing would be to see it published on the central Maven repository, but that's not something that you should do; Twitter should step up and take care of all the Maven users out there.

你怎么得到那个zip文件?最好的办法是在*Maven存储库上发布,但这不是你应该做的;Twitter应该加强并照顾所有的Maven用户。

The next best thing would be to find another public repository that already hosts it well packaged, but I couldn't find one with a nice packaging after a short Google search, maybe you'll have more luck.

下一个最好的方法是找到另一个公共存储库,它已经被很好地打包了,但是在简短的谷歌搜索之后,我找不到一个具有很好的打包,也许您会更幸运。

The third option is to take things into your own hands, and be the one that creates the proper Maven build for that zip and publish it on your own repository.

第三种选择是将事情掌握在自己的手中,并且是创建适当的Maven构建并将其发布到自己的存储库中的工具。

One solution I prefer for building such resource zips is to use the maven assembly plugin, and you can use as an inspiration this maven project: put the sources (js, css and image files) in src/main/resources, and add a simple assembly file that just copies all the files from the resources directory into a zip. Set up a maven repository on a server (a simple HTTP serving machine with sftp access is enough), and do a maven release on that project to upload the artifact on the server.

为建立这样一个解决方案相比,我更喜欢资源拉链是使用maven插件,组装,您可以使用作为一个灵感这maven项目:把来源(js、css和图像文件)在src /主/资源,并添加一个简单的汇编文件,从资源目录的所有文件复制到一个zip。在服务器上设置一个maven存储库(使用sftp访问的简单HTTP服务器就足够了),并在该项目上执行maven版本,以便将工件上传到服务器上。

(You should have a maven repository for releasing your own projects anyway)

(不管怎样,您应该有一个maven存储库来释放您自己的项目)

Next, you would need to declare your repository in your real project's pom file, add a dependency on the bootstrap artifact, and configure the maven-dependency-plugin to unzip it in a location of your choice:

接下来,您需要在真实项目的pom文件中声明您的存储库,添加对引导工件的依赖项,并配置maven- dependent -plugin,在您选择的位置解压缩它:

...
  <repositories><repository>
    <id>bootstrap-repo</id>
    <name>bootstrap public repository</name>
    <url>http://repo.yourhost.net/release</url>
    <releases>
      <enabled>true</enabled>
    </releases>
    <snapshots>
      <enabled>false</enabled>
    </snapshots>
  </repository></repositories>
...
  <dependencies><dependency>
    <groupId>com.twitter.bootstrap</groupId>
    <artifactId>twitter-bootstrap</artifactId>
    <version>2.0.4</version>
    <type>zip</type>
    <scope>runtime</scope>
  </dependency></dependencies>
...
  <build><plugins><plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
      <execution>
        <id>unpack-dependencies</id>
        <phase>prepare-package</phase>
        <goals>
          <goal>unpack</goal>
        </goals>
        <configuration>
          <artifactItems>
            <!-- Unpack the Bootstrap library -->
            <artifactItem>
              <groupId>com.twitter</groupId>
              <artifactId>twitter-bootstrap</artifactId>
              <type>zip</type>
              <outputDirectory>${project.build.directory}/bootstrap</outputDirectory>
            </artifactItem>
          </artifactItems>
        </configuration>
      </execution>
    </executions>
  </plugin></plugins></build>
...

(I'm not sure it would be safe to use com.twitter as the groupId, since they're not the ones publishing that artifact, so use your own groupId)

)我不敢肯定使用com是否安全。twitter作为一个群体,因为他们不是发布那个工件的人,所以使用你自己的群体)

Since your own code just declares a dependency on this twitter-bootstrap artifact, you can keep your main GitHub repository clean of third party code. You don't have to commit the project that builds the twitter-bootstrap artifact at all if you don't want your coding statistics to be influenced, although every line of code you write should be committed.

由于您自己的代码只是声明了对这个twitter-bootstrap工件的依赖,您可以保持您的主GitHub存储库清除第三方代码。如果您不希望您的编码统计数据受到影响,那么您完全不需要提交构建twitter-bootstrap工件的项目,尽管您编写的每一行代码都应该提交。

Alternative: maven-antrun-plugin

An alternative to using a mavenized packaging of third party libraries is to extract those files from their official zip distributions using ant tasks in the maven-antrun-plugin to download the zip and extract the files from it.

使用mavenized打包第三方库的另一种方法是使用maven-antrun-plugin中的ant任务从它们的官方zip分发包中提取这些文件,以下载zip并从中提取文件。

Or, you can use this trick in the twitter-bootstrap maven project in order to avoid copying their source files at all.

或者,您可以在twitter-bootstrap maven项目中使用这个技巧,以避免复制它们的源文件。

#5


1  

The question is fairly old, but for future reference, it should be noted that GitHub added in 2014 a new feature to handle this thing. Possibly thanks to an open issue on the linguist repository.

这个问题相当古老,但是为了以后的参考,应该注意GitHub在2014年添加了一个新特性来处理这个问题。可能要感谢语言学家知识库上的一个开放问题。

This feature allows regarding certain files as libraries, documentation or a specific language.

此特性允许将某些文件视为库、文档或特定语言。

Just add .gitattributes file to your repository. For example, to ignore a library folder, just add the line:

只需将.gitattributes文件添加到存储库中。例如,要忽略一个库文件夹,只需添加一行:

library/* linguist-vendored

Or to set the docs folder, add the line:

或者设置docs文件夹,添加行:

docs/* linguist-documentation

More examples can be found here.

更多的例子可以在这里找到。