如何让SVN忽略文件中的某些代码行?

发布于 2024-12-21 07:07:05 字数 158 浏览 6 评论 0原文

我和我的朋友正在创建一个程序并使用 SVN 来共享代码。问题是我们在其中使用谷歌地图,所以我们需要所有人都有不同的 API 密钥。目前,我们已经在应用程序中注释了 API 密钥行,但如果有人更改该类并使用他的 API 提交,那就很烦人了。

有没有办法告诉不要将某些代码行提交到 SVN?

Me and my Friends are creating a program and using SVN to share code. The problem is that we are using google maps in it, so we need all of us to have different API keys. For now we have commented lines of our API keys in the application, but it is annoying if someone changes that class and commits with his API.

Is there a way to tell not to commit certain lines of code to SVN?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

风情万种。 2024-12-28 07:07:05

从程序中删除硬编码,以便类是通用的(并且可以提交给 SVN)。

相反,请将配置/API 密钥存储在外部配置文件或数据库中。增强代码以在应用程序启动时从存储配置的位置加载配置。


更新:

这是一个用于创建和使用属性文件的非常简单的代码示例:http://www.bartbusschots.ie/blog/?p=360

Remove the hard-coding from your program, so that the classes are universal (and can be committed to SVN).

Instead, store the config / API key in an external config file or database. Enhance the code to load the config from wherever you've stored it when the application starts.


Update:

Here's a very simple code example for creating and using a properties file: http://www.bartbusschots.ie/blog/?p=360

西瑶 2024-12-28 07:07:05

也许你可以使用一个文件.properties,你可以在其中存储所有的API密钥,例如你可以调用一个属性myAPIKey,其他的可以调用如APIKey1,APIKey 2。

如果你这样做,你只需要更改您想要使用 myAPIKey 的属性名称并将其加载到您的 java 类中...

Maybe you can user a file .properties, where you can store the all the API key, for example you can call a property myAPIKey, and the others could be called like APIKey1, APIKey 2.

If you do this, you only have to change the name of the property you want to use to myAPIKey and load it in you java class...

迷途知返 2024-12-28 07:07:05

首先,配置不属于代码。编写 .properties 文件并将密钥和其余属性存储在其中。

之后,您应该

1) 提交属性文件的副本(可能是properties_svn)

2) 如果找不到后者,则使构建过程将properties_svn复制到属性。

3)享受

First of all, configuration does not belong to code. Write a .properties file and store the key and rest of properties there.

After that, you should

1) commit a copy of the properties file (maybe properties_svn)

2) make your build process copy properties_svn to properties if the later is not found.

3) enjoy

失退 2024-12-28 07:07:05

在 SVN 上存储密钥是不好的做法。这就像在那里存储信用卡的密码一样。 O 可能在信用卡本身上写了密码。

这些密钥应该位于您的私有环境中的 SVN 之外。如果您不想创建此类文件,请实现将密钥作为参数或系统属性传递的功能。

It is bad practice to store keys on SVN. It is like storing secret code of your credit card there. O probably writing secret code on the credit card itself.

These keys should be outside SVN on your private environment. If you do not want to create such files implement ability to pass key as an argument or system property.

傲鸠 2024-12-28 07:07:05

正如其他人已经说过的那样,正确的答案是“不要这样做”。

如果您必须,那么最好将所有各种键都放在那里,然后在编译时(例如C预处理器)或运行时(例如基于主机名)。

The correct answer is "don't do that", as others have already said.

If you must surely it's better to put all your various keys in there, and then either select the correct one at compile-time (e.g. C preprocessor) or at run time (e.g. based on hostname).

眼眸 2024-12-28 07:07:05

您应该在代码外部保存这种类型的配置,通常在属性文件中,在运行时注入所需的值。

我通常将一系列属性文件与 Spring 的 org.springframework.beans.factory.config.PropertyPlaceholderConfigurer 结合使用,每个文件都允许根据特定用户的需要覆盖属性值,从而产生以下配置:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
  <property name="ignoreUnresolvablePlaceholders" value="true"/>
  <property name="ignoreResourceNotFound" value="true"/>
  <property name="order" value="1"/>
  <property name="locations">
    <list>
      <value>classpath:my-system.properties</value>
      <value>classpath:my-system-${HOST}.properties</value>
      <value>classpath:my-system-${USERNAME}.properties</value>
   </list>
  </property>
</bean>

如果您不使用 Spring,您可以在如下代码中实现相同的效果:

Properties properties = new Properties();

InputStream systemPropertiesStream = ClassLoader.getSystemResourceAsStream("my-system.properties");
if (systemPropertiesStream != null) 
{
  try
  {
    properties.load(systemPropertiesStream);
  }
  finally 
  {
    systemPropertiesStream.close();
  }  
}

InputStream hostPropertiesStream = ClassLoader.getSystemResourceAsStream("my-system" + InetAddress.getLocalHost().getHostName() + ".properties");
if (hostPropertiesStream != null) 
{
  try
  {
    properties.load(hostPropertiesStream);
  }
  finally 
  {
    hostPropertiesStream.close();
  }  
}

InputStream userPropertiesStream = ClassLoader.getSystemResourceAsStream("my-system" + System.getProperty("user.name") + ".properties");
if (userPropertiesStream != null) 
{
  try
  {
    properties.load(userPropertiesStream);
  }
  finally 
  {
    userPropertiesStream.close();
  }  
}    

You should hold this type of configuration externally to the code, normally in a properties file, injecting the required value at runtime.

I typically use a series of properties files with Spring's org.springframework.beans.factory.config.PropertyPlaceholderConfigurer each allowing the property values to be overridden as needed down to a specific user, resulting in the following config:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
  <property name="ignoreUnresolvablePlaceholders" value="true"/>
  <property name="ignoreResourceNotFound" value="true"/>
  <property name="order" value="1"/>
  <property name="locations">
    <list>
      <value>classpath:my-system.properties</value>
      <value>classpath:my-system-${HOST}.properties</value>
      <value>classpath:my-system-${USERNAME}.properties</value>
   </list>
  </property>
</bean>

If you're not using Spring you can achieve just the same effect in code like this:

Properties properties = new Properties();

InputStream systemPropertiesStream = ClassLoader.getSystemResourceAsStream("my-system.properties");
if (systemPropertiesStream != null) 
{
  try
  {
    properties.load(systemPropertiesStream);
  }
  finally 
  {
    systemPropertiesStream.close();
  }  
}

InputStream hostPropertiesStream = ClassLoader.getSystemResourceAsStream("my-system" + InetAddress.getLocalHost().getHostName() + ".properties");
if (hostPropertiesStream != null) 
{
  try
  {
    properties.load(hostPropertiesStream);
  }
  finally 
  {
    hostPropertiesStream.close();
  }  
}

InputStream userPropertiesStream = ClassLoader.getSystemResourceAsStream("my-system" + System.getProperty("user.name") + ".properties");
if (userPropertiesStream != null) 
{
  try
  {
    properties.load(userPropertiesStream);
  }
  finally 
  {
    userPropertiesStream.close();
  }  
}    
一身仙ぐ女味 2024-12-28 07:07:05

通常这些东西不是源代码版本控制工具的一部分。大多数开发人员通过使用构建系统来解决这个或类似的问题。例如行家。

例如,使用 Maven,有人可以为具有不同 api 密钥或文件夹引用等的不同用户定义具有不同属性文件的不同配置文件。

Usually such things are not part of a source code versioning tool. Most developers solve this or similar problems with using a build system. E.g. maven.

With maven for example someone would define different profiles with different property files for different users having different api keys or folder references etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文